JSP learning two (request forwarding and redirection)

Source: Internet
Author: User

One:

1. Introduction of Knowledge points

1). Essential difference: The forwarding of the request only makes one request, while the redirect makes two requests.

Specific:

①. Forwarding of requests: The Address bar is the address where the request was first made.
REDIRECT requested: The Address bar is no longer the initial request address. Address bar is the last address that responds

②. Request forwarding: In the final Servlet, the request object and the requested request in transit are the same object.
Request redirection: In the final Servlet, the request object and the requested request in transit are not the same object.

③. Forwarding of requests: resources that can only be forwarded to the current WEB app
REDIRECT requested: can be redirected to any resource.

④. Forwarding of requests:/represents the root directory of the current WEB app
REDIRECT requested:/represents the root directory of the current WEB site.

Two: procedure

1.web.xml (about configuration using annotations)

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns: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 files (two links representing two points of knowledge)

1<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "2pageencoding= "Iso-8859-1"%>3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >456<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">7<title>insert title here</title>89<body>Ten<!--Request Forwarding-- One<a href= "ForwardServlet" >Forward</a> A      -<br><br> -      the<!--redirects-- -<a href= "Redirectservlet" >RedirectServlet</a> -</body> -

3.forwardservlet.java

1  PackageServlets;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.RequestDispatcher;6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.annotation.WebServlet;8 ImportJavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten ImportJavax.servlet.http.HttpServletResponse; One  A /** - * Servlet Implementation class ForwardServlet -  */ the@WebServlet ("/forwardservlet") -  Public classForwardServletextendsHttpServlet { -     Private Static Final LongSerialversionuid = 1L; -         +     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -System.out.println ("ForwardServlet doget"); +         //Request Forwarding AString path= "Nextservlet"; atRequestDispatcher requestdispatcher=request.getrequestdispatcher ("/" +path); - Requestdispatcher.forward (request, response); -     } -  -}

4.nextservlet.java

1  PackageServlets;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten /** One * Servlet Implementation class Nextservlet A  */ -@WebServlet ("/nextservlet") -  Public classNextservletextendsHttpServlet { the     Private Static Final LongSerialversionuid = 1L; -       -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -System.out.println ("Nextservlet doget"); +     } -}

5.redirectservlet.java

1  PackageServlets;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten /** One * Servlet Implementation class Redirectservlet A  */ -@WebServlet ("/redirectservlet") -  Public classRedirectservletextendsHttpServlet { the     Private Static Final LongSerialversionuid = 1L; -     -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -System.out.println ("Redirectservlet doget"); +          -         // redirect +String path= "Nextservlet"; A response.sendredirect (path); at     } -}

6. Effects

Click the link individually.

  

JSP learning two (request forwarding and redirection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.