java實(shí)現(xiàn)響應(yīng)重定向發(fā)送post請求操作示例
本文實(shí)例講述了java實(shí)現(xiàn)響應(yīng)重定向發(fā)送post請求操作。分享給大家供大家參考,具體如下:
關(guān)于重定向我們用的比較多的還是redirect:重定向,默認(rèn)發(fā)送的get請求。
return 'redirect:/index';
但有時(shí)候請求地址必須為post請求,比如security登錄就只能接收post請求,下面來看一下如何后臺如何發(fā)送post請求響應(yīng)重定向。
首先可以定義一個(gè)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(); }}
實(shí)例化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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果3. 刪除docker里建立容器的操作方法4. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法5. 如果你恨一個(gè)程序員,忽悠他去做iOS開發(fā)6. IDEA調(diào)試源碼小技巧之辨別抽象類或接口多種實(shí)現(xiàn)類的正確路徑7. .Net中的Http請求調(diào)用詳解(Post與Get)8. java使用xfire搭建webservice服務(wù)的過程詳解9. JS如何在數(shù)組指定位置插入元素10. Java源碼解析之ClassLoader
