java實現響應重定向傳送post請求操作示例
阿新 • • 發佈:2020-04-13
本文例項講述了java實現響應重定向傳送post請求操作。分享給大家供大家參考,具體如下:
關於重定向我們用的比較多的還是redirect:重定向,預設傳送的get請求。
return "redirect:/index";
但有時候請求地址必須為post請求,比如security登入就只能接收post請求,下面來看一下如何後臺如何傳送post請求響應重定向。
首先可以定義一個map,用於存放參數鍵值對
Map<String,String> parameter = new HashMap<String,String>();
新增引數方法
public void setParameter(String key,String value) { this.parameter.put(key,value); }
傳送請求程式碼:
//url引數為請求地址 public void sendByPost(String url) throws IOException { this.response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = this.response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD>"); out.println(" <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">"); out.println(" <TITLE>loading</TITLE>"); out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html charset=GBK\">\n"); out.println(" </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(); }
RedirectWithPost請求類全部程式碼
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; /** * 用POST方式 重定向 * * @author royFly */ public class RedirectWithPost { Map<String,String>(); HttpServletResponse response; public RedirectWithPost(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"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = this.response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD>"); out.println(" <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">"); out.println(" <TITLE>loading</TITLE>"); out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html charset=GBK\">\n"); out.println(" </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(); } }
例項化RedirectWithPost請求類
RedirectWithPost redirectWithPost = new RedirectWithPost(response); //redirectUrl請求地址 String redirectUrl = "/login";
新增請求引數
redirectWithPost.setParameter("username",nickname); redirectWithPost.setParameter("password",openid);
呼叫方法,傳送請求
redirectWithPost.sendByPost(redirectUrl);
更多關於java相關內容感興趣的讀者可檢視本站專題:《Java Socket程式設計技巧總結》、《Java檔案與目錄操作技巧彙總》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》和《Java快取操作技巧彙總》
希望本文所述對大家java程式設計有所幫助。