午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

java實現(xiàn)響應重定向發(fā)送post請求操作示例

瀏覽:119日期:2022-09-03 08:50:42

本文實例講述了java實現(xiàn)響應重定向發(fā)送post請求操作。分享給大家供大家參考,具體如下:

關于重定向我們用的比較多的還是redirect:重定向,默認發(fā)送的get請求。

return 'redirect:/index';

但有時候請求地址必須為post請求,比如security登錄就只能接收post請求,下面來看一下如何后臺如何發(fā)送post請求響應重定向。

首先可以定義一個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);

調用方法,發(fā)送請求

redirectWithPost.sendByPost(redirectUrl);

更多關于java相關內容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

標簽: Java
相關文章:
主站蜘蛛池模板: 噜噜色综合 | 999成人网 | av免费毛片 | 91在线导航 | 欧美日韩在线视频免费播放 | 99色网站 | 国产精品嫩草影院俄罗斯 | 天天爽天天做 | 国产传媒自拍 | 黄网站在线播放 | 亚洲香蕉视频 | 粉嫩av四季av绯色av | 欧美日韩综合视频 | 国产毛片av| 中文字幕在线永久 | www.黄色网址 | 国产精品欧美在线 | 91免费福利 | 欧美伊人久久 | 久久久免费观看视频 | 亚洲福利天堂 | 欧美黑人猛交 | 国产精品性 | 97超碰人人干 | 不卡视频一区 | 二区三区视频 | 99av国产精品欲麻豆 | 青娱乐精品视频 | 9.1片黄在线观看 | 久草a在线| www.久久久久久久久久 | 青青国产在线 | 天天插天天舔 | 久久精品久久久久久 | 国产999久久久 | 亚洲激情视频 | 日日干日日 | 国产有码在线 | 日本不卡视频在线 | 99久久久国产精品免费蜜臀 | 国产91在线播放精品91 |