Javaweb-HttpServletResponse的sendRedirectch重定向方式
一 、sendRedirect
1.1 重定向過程
當(dāng)使用HttpServletResponse的sendRedirect()時(shí)會(huì)發(fā)生重定向時(shí),服務(wù)器會(huì)在Servlet中設(shè)置HTTP狀態(tài)碼為301以及Location標(biāo)頭返回給瀏覽器,瀏覽器收到這個(gè)標(biāo)頭以后會(huì)再一次使用GET方法請(qǐng)求重定向的URL,因此地址欄的URL會(huì)發(fā)生變化。
1.2 sendRedirect使用方法
response.sendRedirect(URL url);
二、實(shí)驗(yàn)
2.1使用sendRedirect()進(jìn)行重定向,同時(shí)使用HttpServletRequest中的setAttribute進(jìn)行屬性設(shè)置
在瀏覽器中輸入http://localhost:8088/send_redirect.html
send_redirect.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/send_redirect" method="post">
Name:<input type="text" name="name"/><br/>
Submit:<input type="submit" value="Submit By Post"/>
</form>
</body>
</html>在網(wǎng)頁的Name這一欄輸入Zoubaitao

點(diǎn)擊Submit By Post,瀏覽器的地址欄會(huì)變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會(huì)去服務(wù)器的web.xml找到對(duì)應(yīng)的url-pattern最后找到對(duì)應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對(duì)應(yīng)的Servlet;
SendRedirectServlet.java
package httpservletresponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/send_redirect")
public class SendRedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
response.setContentType("text/html");
request.setAttribute("name",name);
//setAttribute("name",name);
response.sendRedirect("http://localhost:8088/welcome.jsp");
}
}welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Welcome<br/>
${name}
</body>
</html>最后輸出的結(jié)果為

結(jié)果分析:
- 我們發(fā)現(xiàn)我們?cè)趓equest中設(shè)置的name屬性并沒有傳遞到welcome的request對(duì)象中。
- 而welcome.jsp中的request對(duì)象不存在一個(gè)name 屬性值,所以不輸出
2.2使用sendRedirect()進(jìn)行重定向,同時(shí)使用HttpServletRequest中的setAttribute進(jìn)行屬性設(shè)置
在瀏覽器中輸入http://localhost:8088/send_redirect.html
send_redirect.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/send_redirect" method="post">
Name:<input type="text" name="name"/><br/>
Submit:<input type="submit" value="Submit By Post"/>
</form>
</body>
</html>在網(wǎng)頁的Name這一欄輸入Yanzi

點(diǎn)擊Submit By Post,瀏覽器的地址欄會(huì)變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會(huì)去服務(wù)器的web.xml找到對(duì)應(yīng)的url-pattern最后找到對(duì)應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對(duì)應(yīng)的Servlet;
SendRedirectServlet.java
package httpservletresponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/send_redirect")
public class SendRedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
response.setContentType("text/html");
this.getServletContext.setAttribute("name",name);
//setAttribute("name",name);
response.sendRedirect("http://localhost:8088/welcome.jsp");
}
}welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Welcome<br/>
${name}
</body>
</html>結(jié)果輸出

結(jié)果分析:
- 我們通過ServletContext來設(shè)置的屬性,最后能夠在重定向的welcome.jsp中得到這個(gè)屬性值,最終輸出在瀏覽器上。
- 因此如果我們需要在重定向的時(shí)候設(shè)置共享的變量,那么我們需要使用的ServletContext來設(shè)置屬性。
- 同時(shí)我們可以看到URL地址欄變成了我們?cè)O(shè)置的重定向的地址
2.3使用sendRedirect()進(jìn)行重定向,但是我們重定向的資源放在WEB-INF文件夾下
在瀏覽器中輸入http://localhost:8088/send_redirect.html
send_redirect.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/send_redirect" method="post">
Name:<input type="text" name="name"/><br/>
Submit:<input type="submit" value="Submit By Post"/>
</form>
</body>
</html>在網(wǎng)頁的Name這一欄輸入Yanzi

點(diǎn)擊Submit By Post,瀏覽器的地址欄會(huì)變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會(huì)去服務(wù)器的web.xml找到對(duì)應(yīng)的url-pattern最后找到對(duì)應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對(duì)應(yīng)的Servlet;
SendRedirectServlet.java
package httpservletresponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/send_redirect")
public class SendRedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
response.setContentType("text/html");
this.getServletContext.setAttribute("name",name);
//資源路徑為WEB-INF文件夾下
response.sendRedirect("http://localhost:8088/WEB-INF/welcome.jsp");
}
}welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Welcome<br/>
${name}
</body>
</html>結(jié)果輸出

結(jié)果分析:
- 我們將我們重定向的資源放在了WEB-INF文件夾下,所以瀏覽器是無法訪問到的。
- 這是重定向和轉(zhuǎn)發(fā)之間的一個(gè)不同點(diǎn),轉(zhuǎn)發(fā)有服務(wù)器自己處理轉(zhuǎn)發(fā)請(qǐng)求,但是重定向是瀏覽器對(duì)重定向的資源再一次進(jìn)行請(qǐng)求。
- 這從請(qǐng)求次數(shù)上,轉(zhuǎn)發(fā)只請(qǐng)求一次,但是重定向至少請(qǐng)求兩次。
三、總結(jié)
RequestDispatcher的forward()和HttPServletResponse的sendRedirect()之間的不同
1) foward()就可以訪問WEB-INF文件夾下的資源,而sendRedirect()不能重定向在WEB-INF文件夾下的資源
2) forward()進(jìn)行轉(zhuǎn)發(fā)時(shí)是服務(wù)器內(nèi)部進(jìn)行處理,因此瀏覽器不知道發(fā)生的變化,所以瀏覽器的URL不會(huì)發(fā)生變化,而sendRedirect()會(huì)發(fā)生變 換,是因?yàn)闉g覽器會(huì)從服務(wù)器得到301狀態(tài)碼以后會(huì)再一次按照重定向的URL以get的方式請(qǐng)求服務(wù)器。所以服務(wù)器地址欄的URL發(fā)生變化
3) forward()可以通過HttpServletRequest的setAttribute()和ServletContex的setAttribute()設(shè)置共享屬性。而sendRedirect()只能通過ServletContext()的setAttribute()進(jìn)行訪問屬性(其實(shí)二者另外還都可以使用Session來存儲(chǔ)共享的數(shù)據(jù))
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort)
這篇文章主要介紹了java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
springcloud-feign調(diào)用報(bào)錯(cuò)問題
這篇文章主要介紹了springcloud-feign調(diào)用報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java高效實(shí)現(xiàn)PDF與OFD的相互轉(zhuǎn)換的示例代碼
在數(shù)字化辦公日益普及的今天,文檔格式的標(biāo)準(zhǔn)化與跨平臺(tái)兼容性成為企業(yè)和開發(fā)者面臨的重要挑戰(zhàn),OFD作為國家標(biāo)準(zhǔn),在政務(wù)、金融等領(lǐng)域具有不可替代的地位,而PDF則以其廣泛的兼容性成為日常交流的首選本文將深入探討如何高效地實(shí)現(xiàn)PDF與OFD的相互轉(zhuǎn)換2025-10-10
Java JDBC數(shù)據(jù)庫連接失敗的7種常見原因及解決方案
Java JDBC(Java Database Connectivity)是Java平臺(tái)中用于執(zhí)行SQL語句的標(biāo)準(zhǔn)API,它為開發(fā)者提供了與各種關(guān)系型數(shù)據(jù)庫進(jìn)行交互的能力,本文為大家介紹了Java JDBC數(shù)據(jù)庫連接失敗的7種常見原因及解決方案,需要的朋友可以參考下2025-11-11
ConditionalOnProperty配置swagger不生效問題及解決
這篇文章主要介紹了ConditionalOnProperty配置swagger不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
淺談java String.split丟失結(jié)尾空字符串的問題
下面小編就為大家?guī)硪黄獪\談java String.split丟失結(jié)尾空字符串的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
盤點(diǎn)SpringBoot中@Async注解的遇到的坑點(diǎn)及解決辦法
SpringBoot是一個(gè)流行的Java開發(fā)框架,在異步編程方面,Spring Boot提供了@Async注解,它能夠讓方法異步執(zhí)行,然而,在使用@Async注解時(shí),有一些潛在的坑需要注意,本文將深入探討Spring Boot中使用@Async注解時(shí)可能遇到的8大坑點(diǎn),并提供相應(yīng)的解決方案2024-03-03

