色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

java實現響應重定向發送post請求操作示例

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

本文實例講述了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> 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';

添加請求參數

redirectWithPost.setParameter('username', nickname);redirectWithPost.setParameter('password', openid);

調用方法,發送請求

redirectWithPost.sendByPost(redirectUrl);

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

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

標簽: Java
相關文章:
主站蜘蛛池模板: 成人五级毛片免费播放 | 色樱桃影院亚洲精品影院 | 日本综合欧美一区二区三区 | 精品亚洲成a人在线观看 | 欧美成亚洲 | 欧美国产成人一区二区三区 | 日韩美三级 | 美女张开双腿让男人桶视频免费 | 日韩精品永久免费播放平台 | 久久三级毛片 | 欧美日韩一区二区三区在线 | 欧美综合精品一区二区三区 | 孕妇交 | 亚洲免费网站在线观看 | 国产夫妇精品自在线 | 国产真实生活伦对白 | 国产一级一级 | 欧美日韩亚洲精品一区 | 欧美性高清bbbbbbxxxxx | 亚洲m男在线中文字幕 | 成人五级毛片免费播放 | 好看的看黄a大片爽爽影院 好男人天堂网 | 久久精品a亚洲国产v高清不卡 | 国产精品视频久久久 | 国产综合在线观看 | 久久亚洲精品无码观看不卡 | 外国成人网在线观看免费视频 | 一区二区三区免费视频网站 | 国产成人啪一区二区 | 亚洲国产欧美在线人成精品一区二区 | 成人亚洲国产 | 亚洲精品自拍 | 欧美日本道免费一区二区三区 | 成人中文字幕在线观看 | 午夜大片免费男女爽爽影院久久 | 一区二区三区影视 | 午夜精品视频在线观看美女 | 亚洲人成亚洲人成在线观看 | 国产成人毛片毛片久久网 | 亚洲天堂久久久 | 久久99爰这里有精品国产 |