码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-08-24 01:12:20      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:insert   ima   请求   code   doget   区别   XML   ring   splay   

一:

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.效果

  分别点击一次链接。

  技术分享

 

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

标签:insert   ima   请求   code   doget   区别   XML   ring   splay   

原文地址:http://www.cnblogs.com/juncaoit/p/7420940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!