Response重定向实现参数隐藏

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_41535326/article/details/89151004

最近在弄一个SSH项目,前期已经做好了,现在的需求是进行单点登陆实现,涉及到重定向跳转(带有参数那种)情况,但是不能在地址栏上出现参数的信息,需要进行参数的隐藏跳转。仅供大家参考学习!好了,直接上代码:

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpClientPostFs {
    Map<String, String> parameter=new HashMap<String, String>();
    HttpServletResponse response;

    public HttpClientPostFs()
    {
    }
    public HttpClientPostFs(HttpServletResponse response)
    {
        this.response=response;
    }
    public void setParameter(String key, String value)
    {
        this.parameter.put(key, value);
    }
    public void sendByPost(String url) throws IOException
    {
        this.response.setContentType("text/html");
        PrintWriter out = this.response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(" <HEAD><TITLE>sender</TITLE></HEAD>");
        out.println(" <BODY>");
        out.println("<form name=\"submitForm\" action=\""+url+"\" method=\"post\">");
        Iterator<String> it=this.parameter.keySet().iterator();
        while(it.hasNext())
        {
            String key=it.next();
            out.println("<input type=\"hidden\" name=\""+key+"\" value=\""+this.parameter.get(key)+"\"/>");
        }
        out.println("</from>");
        out.println("<script>window.document.submitForm.submit();</script> ");
        out.println(" </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}

上面的是一个工具类的所有内容,下面是调用实现参数隐藏跳转代码展示:

HttpClientPostFs http = new HttpClientPostFs(ServletActionContext.getResponse());
http.setParameter("usercode", "123456");//将参数封装到这个里面,以键值对的形式存在
http.setParameter("password", "123456");
http.sendByPost(url);//进行跳转

猜你喜欢

转载自blog.csdn.net/weixin_41535326/article/details/89151004