Response重定向實現引數隱藏
阿新 • • 發佈:2018-12-18
最近在弄一個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);//進行跳轉