Javaweb-HttpServletResponse的sendRedirectch重定向方式
一 、sendRedirect
1.1 重定向過程
當(dāng)使用HttpServletResponse的sendRedirect()時(shí)會發(fā)生重定向時(shí),服務(wù)器會在Servlet中設(shè)置HTTP狀態(tài)碼為301以及Location標(biāo)頭返回給瀏覽器,瀏覽器收到這個(gè)標(biāo)頭以后會再一次使用GET方法請求重定向的URL,因此地址欄的URL會發(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,瀏覽器的地址欄會變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會去服務(wù)器的web.xml找到對應(yīng)的url-pattern最后找到對應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對應(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)我們在request中設(shè)置的name屬性并沒有傳遞到welcome的request對象中。
- 而welcome.jsp中的request對象不存在一個(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,瀏覽器的地址欄會變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會去服務(wù)器的web.xml找到對應(yīng)的url-pattern最后找到對應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對應(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地址欄變成了我們設(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,瀏覽器的地址欄會變成http://localhost:8088/send_redirect
服務(wù)器根據(jù)”/send_redirect”就會去服務(wù)器的web.xml找到對應(yīng)的url-pattern最后找到對應(yīng)的Servlet來處理;或者是根據(jù)@WebServlet來查找對應(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ā)請求,但是重定向是瀏覽器對重定向的資源再一次進(jìn)行請求。
- 這從請求次數(shù)上,轉(zhuǎn)發(fā)只請求一次,但是重定向至少請求兩次。
三、總結(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不會發(fā)生變化,而sendRedirect()會發(fā)生變 換,是因?yàn)闉g覽器會從服務(wù)器得到301狀態(tài)碼以后會再一次按照重定向的URL以get的方式請求服務(wù)器。所以服務(wù)器地址欄的URL發(fā)生變化
3) forward()可以通過HttpServletRequest的setAttribute()和ServletContex的setAttribute()設(shè)置共享屬性。而sendRedirect()只能通過ServletContext()的setAttribute()進(jìn)行訪問屬性(其實(shí)二者另外還都可以使用Session來存儲共享的數(shù)據(jù))
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java.lang.String和java.util.NClob互相轉(zhuǎn)換方式
這篇文章主要介紹了java.lang.String和java.util.NClob互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot項(xiàng)目mapper無法自動裝配未找到?UserMapper?類型的Bean解決辦法
這篇文章給大家介紹了springboot項(xiàng)目mapper無法自動裝配,未找到?‘userMapper‘?類型的?Bean解決辦法(含報(bào)錯原因),文章通過圖文結(jié)合的方式介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
MyBatis中調(diào)用存儲過程和函數(shù)的實(shí)現(xiàn)示例
在MyBatis中調(diào)用存儲過程和函數(shù)是一個(gè)相對高級的特性,本文主要介紹了MyBatis中調(diào)用存儲過程和函數(shù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
解決微服務(wù)中關(guān)于用戶token處理到的坑
這篇文章主要介紹了解決微服務(wù)中關(guān)于用戶token處理到的坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)...2006-12-12
Java字符串拼接+和StringBuilder的比較與選擇
Java 提供了兩種主要的方式:使用 "+" 運(yùn)算符和使用 StringBuilder 類,本文主要介紹了Java字符串拼接+和StringBuilder的比較與選擇,感興趣的可以了解一下2023-10-10
Java通過SMS短信平臺實(shí)現(xiàn)發(fā)短信功能 含多語言
這篇文章主要為大家詳細(xì)介紹了Java通過SMS短信平臺實(shí)現(xiàn)發(fā)短信功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Java批量轉(zhuǎn)換文件編碼格式的實(shí)現(xiàn)方法及實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn) 批量轉(zhuǎn)換文件編碼格式的方法及實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

