JSP的学习二(请求转发与 重定向)

一:

1.介绍知识点

  1). 本质区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求.

  具体:

    ①. 请求的转发: 地址栏是初次发出请求的地址.
       请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址

    ②. 请求转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象.
          请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.

    ③. 请求的转发: 只能转发给当前 WEB 应用的的资源
       请求的重定向: 可以重定向到任何资源.

    ④. 请求的转发: / 代表的是当前 WEB 应用的根目录
       请求的重定向: / 代表的是当前 WEB 站点的根目录.

 

 

二:程序

1.web.xml(关于配置使用注解了)

1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5     id="WebApp_ID" version="3.1">
6     <display-name>JspTest</display-name>
7 </web-app>

 

2.jsp文件(两种链接代表两种知识点)

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <!-- 请求转发 -->
11     <a href="forwardServlet">Forward</a>
12     
13     <br><br>
14     
15     <!-- 重定向 -->
16     <a href="redirectServlet">RedirectServlet</a>
17 </body>
18 </html>

 

3.ForwardServlet.java

 1 package Servlets;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.RequestDispatcher;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class ForwardServlet
14  */
15 @WebServlet("/forwardServlet")
16 public class ForwardServlet extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20         System.out.println("ForwardServlet doGet");
21         //请求转发
22         String path="nextServlet";
23         RequestDispatcher requestDispatcher=request.getRequestDispatcher("/"+path);
24         requestDispatcher.forward(request, response);
25     }
26 
27 }

 

4.NextServlet.java

 1 package Servlets;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class NextServlet
12  */
13 @WebServlet("/nextServlet")
14 public class NextServlet extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16      
17     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         System.out.println("NextServlet doGet");
19     }
20 }

 

5.RedirectServlet.java

 1 package Servlets;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class RedirectServlet
12  */
13 @WebServlet("/redirectServlet")
14 public class RedirectServlet extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16    
17     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         System.out.println("RedirectServlet doGet");
19         
20         //重定向
21         String path="nextServlet";
22         response.sendRedirect(path);
23     }
24 }

 

6.效果

  分别点击一次链接。

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值