zoukankan      html  css  js  c++  java
  • 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.效果

      分别点击一次链接。

      

  • 相关阅读:
    (教程)怎么避免拼多多比价订单行为
    Ubuntu下搭建apache2+GGI环境
    搭建k8s
    我的2021年总结
    工作三年的一些感悟
    xshell6+xftp6免安装破解版
    centos7启动docker容器时提示Error response from daemon: Unknown runtime specified dockerrunc
    解决一个C#中定时任务被阻塞问题
    工程中实际问题解决两例——基于C#
    解决一次gitlab因异常关机导致启动失败
  • 原文地址:https://www.cnblogs.com/juncaoit/p/7420940.html
Copyright © 2011-2022 走看看