您好,登錄后才能下訂單哦!
小編這次要給大家分享的是java如何實現(xiàn)響應(yīng)重定向發(fā)送post請求,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
關(guān)于重定向我們用的比較多的還是redirect:重定向,默認(rèn)發(fā)送的get請求。
return "redirect:/index";
但有時候請求地址必須為post請求,比如security登錄就只能接收post請求,下面來看一下如何后臺如何發(fā)送post請求響應(yīng)重定向。
首先可以定義一個map,用于存放參數(shù)鍵值對
Map<String, String> parameter = new HashMap<String, String>();
添加參數(shù)方法
public void setParameter(String key, String value) { this.parameter.put(key, value); }
發(fā)送請求代碼:
//url參數(shù)為請求地址 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> parameter = new HashMap<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";
添加請求參數(shù)
redirectWithPost.setParameter("username", nickname); redirectWithPost.setParameter("password", openid);
調(diào)用方法,發(fā)送請求
redirectWithPost.sendByPost(redirectUrl);
看完這篇關(guān)于java如何實現(xiàn)響應(yīng)重定向發(fā)送post請求的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。