Response重定向實現參數隱藏

最近在弄一個SSH項目,前期已經做好了,現在的需求是進行單點登陸實現,涉及到重定向跳轉(帶有參數那種)情況,但是不能在地址欄上出現參數的信息,需要進行參數的隱藏跳轉。由於時間比較急,本人沒來得及開發一個小工具,這次用的別人以前寫好的工具類進行參數隱藏。放在這裏好讓自己積累一些工具類,也方便大家參考!好了,直接上代碼:

package com.example.Utils;

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);//進行跳轉

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章