欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年04月11日 08:53:24   作者:一枕江風(fēng)  
這篇文章主要介紹了java實現(xiàn)響應(yīng)重定向發(fā)送post請求操作,結(jié)合實例形式分析了java請求響應(yīng)、重定向及數(shù)據(jù)處理相關(guān)操作技巧,需要的朋友可以參考下

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

關(guān)于重定向我們用的比較多的還是redirect:重定向,默認發(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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • Mybatis-Plus中的查詢指定字段

    Mybatis-Plus中的查詢指定字段

    在使用Mybatis-Plus進行數(shù)據(jù)查詢時,可以通過指定字段來優(yōu)化查詢效率,方法一和方法二分別執(zhí)行不同的SQL語句,其中方法二在執(zhí)行時通常會更高效,因為它可能通過減少數(shù)據(jù)處理量和優(yōu)化查詢結(jié)構(gòu)來提升性能,比較兩種方法的SQL執(zhí)行情況
    2024-09-09
  • SpringMVC源碼之HandlerMapping處理器映射器解析

    SpringMVC源碼之HandlerMapping處理器映射器解析

    這篇文章主要介紹了SpringMVC源碼之HandlerMapping處理器映射器解析,在Spring?MVC中,HandlerMapping處理器映射器用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下
    2023-08-08
  • Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解

    Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解

    這篇文章主要介紹了Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException

    解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.fac

    這篇文章主要介紹了解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • WeakHashMap?和?HashMap?區(qū)別及使用場景

    WeakHashMap?和?HashMap?區(qū)別及使用場景

    這篇文章主要為大家介紹了WeakHashMap?和?HashMap?的區(qū)別是什么以及何時使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • SpringBoot整合SpringDataJPA

    SpringBoot整合SpringDataJPA

    這篇文章主要介紹了SpringBoot整合SpringDataJPA代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-07-07
  • IDEA 2020.1打開時閃退的問題及解決方法(完美解決方法)

    IDEA 2020.1打開時閃退的問題及解決方法(完美解決方法)

    這篇文章主要介紹了IDEA 2020.1打開時閃退問題及解決方法,本文給大家分享我的處理方案,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • java實現(xiàn)的日期時間轉(zhuǎn)換工具類完整示例

    java實現(xiàn)的日期時間轉(zhuǎn)換工具類完整示例

    這篇文章主要介紹了java實現(xiàn)的日期時間轉(zhuǎn)換工具類,結(jié)合完整實例形式分析了java針對日期時間常見的轉(zhuǎn)換、計算、格式化等相關(guān)操作與封裝技巧,需要的朋友可以參考下
    2019-10-10
  • SpringMVC之簡單的增刪改查示例(SSM整合)

    SpringMVC之簡單的增刪改查示例(SSM整合)

    本篇文章主要介紹了SpringMVC之簡單的增刪改查示例(SSM整合),這個例子是基于SpringMVC+Spring+Mybatis實現(xiàn)的。有興趣的可以了解一下。
    2017-03-03
  • SpringBoot?+?Redis如何解決重復(fù)提交問題(冪等)

    SpringBoot?+?Redis如何解決重復(fù)提交問題(冪等)

    在開發(fā)中,一個對外暴露的接口可能會面臨瞬間的大量重復(fù)請求,本文就介紹了SpringBoot + Redis如何解決重復(fù)提交問題,具有一定的參考價值,感興趣的可以了解一下
    2021-12-12

最新評論