About Sendredirect,forward and include differences in Java

Source: Internet
Author: User

In the Javaweb page jumps generally have three kinds of forms, sendredirect,forward and include, what is the difference of three?

I'll explain it first, and then take a small example

First, Sendredirect

How to use

Response.sendredirect ();

The server sends a status code according to logic, tells the browser to request the specified address, and places the required parameters in the forwarding address. Because it is a new application, the original request cannot be read, can be replaced with the session, or use the include, and forward instead

Second, forward

How to use

Request.getrequestdispatcher ("/first.jsp"). Forward (request, response);

The page will be the content of the first.jsp, the address bar does not change

You can use the set properties

Iii. include

How to use

Request.getrequestdispatcher ("/first.jsp"). Include (request, response);

The page will contain both the current page and the first.jsp content.

You can use the set properties

Here is an example

index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >can get ID:<%= Request.getparameter ("id")%></body>

first.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

second.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

third.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

fourth.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Next run the server (using TomCat8.0)

Access project

Click on the first link

The page does not change, but the console has an output indicating that it has been accessed

We tried to pass a parameter to the past id=123456

Cannot get ID, description is a request that the browser re-issued, requests have been destroyed, this is a new ask

Put the flowchart below

Click on a third connection

The page has not changed, but the address bar has changed

Let's pass a parameter in.

Yes, the ID can be displayed, indicating that the same request

Put the flowchart below

Because forward is a server internal jump, will take the request together to the next page, so ID can get

Then click on the Fifth link

The page changes, including the contents of third.jsp and fourth.jsp

Let's pass a parameter.

can get request

Here is the flowchart

I have limited knowledge, and if there are errors, I hope to point out

This article is original, reproduced please declare the source.

About Sendredirect,forward and include differences in Java

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.