Forward, redirect, and difference and simple session object, redirect session

Source: Internet
Author: User

Forward, redirect, and difference and simple session object, redirect session

1. Forwarding
Purpose: Share the requested data during interaction between multiple pages.
Process: The Web Server gives the processing right of a request to another resource, which belongs to the same access request and response process. Therefore, the information of the request object will not be lost.

2. Redirection
Redirection means to redirect various network requests to other locations through various methods (such as webpage redirection and Domain Name Redirection, the change in route selection is also a redirection of data packets through a route .)
Redirection uses the sendRedirect () method of the response object.
Process: The Web server returns a response to the browser. The browser accepts the response and then sends a new http request to the server. This is two different requests, the request information of the previous request will be lost.

Example: Login: <body>

<Form name = "form" method = "post" action = "control. jsp">

Username: <input type = "text" name = "username">

Password: <input type = "password" name = "pwd">

<Input type = "submit" value = "login">

</Form>

</Body>

Welcome Page:

<Head> <title> welcome page </title>

<Body> welcome to this page </body>

</Html>

Control. jsp page:

<Head> logon handling page

<Body>

<% Request. setCharacterEncoding ("UTF-8 ");

String name = request. geyParameter ("userName ");

String pwd = request. geyParameter ("pwd ");

If (name. equals ("admin") & pwd. equals ("123 ")){

Request. getrequestDispatcher ("welcome. jsp"). forword (request, reponse); // forward

} Else {

Reponse. sendRedirect ("Login. jsp"); // redirect

}

%>

</Body>

</Html>

3. What is the difference between redirection and forwarding:
<1> there is an important difference between redirection and forwarding: when forwarding is used, the JSP Container uses an internal method to call the target page, and the new page continues to process the same request, the browser will not know this process. In contrast, redirection means that the first page notifies the browser to send a new page request. Because when you use redirection, the URL displayed in the browser will change to the URL of the new page, and the URL will remain unchanged when forwarding is used. Redirection is slower than forwarding because the browser has to send a new request. At the same time, because the redirection method generates a new request, after a redirection, the objects in the request will not be available.
How can I choose redirection or forwarding? Generally, forwarding is faster and objects in the request can be kept. Therefore, it is the first choice. However, after forwarding, the URL in the browser still points to the start page. If you reload the current page, the start page will be called again. If you do not want to see such a situation, select forwarding.
<2> do not use the session scope only to pass the variable to the next page. It will increase the scope of the variable without reason. Forwarding may help you solve this problem.
Redirection: all variables stored in previous requests are invalid and enter a new request scope.
Forward: the variables stored in the previous request will not expire, Just Like splicing the two pages together.
<3> forwarding works on the server. Multiple pages of the submitted information are transmitted through the forword () method of the RequestDispatcher object. redirection is used by the client to redirect pages by requesting a new address.

4. Session

A session is a call between the user and the server through a browser.

Session objects are used to store the information required for a specific user session. The introduction of session objects aims to make up for the shortcomings of the http protocol. http is a stateless protocol.

Session Object failure mode: manual Effect

Implementation Method of timeout and invalidation: <1> On the page: Set the session inactivity time using the setMaxInactiveInterval () method. Example: <% session. setAttribute ("Login", "admin"); session. setMaxInactiveInterval (600); response. sendRedirect ("admin. jsp "); %>

<2> in the project web. set in xml. <session-config> <session-timeout> 10 <session-timeout> </session-config> the unit is 10 minutes. Set this parameter to 0, -1 indicates never timeout <3> set in the application server, In the Tomcat directory/conf/web. the <session-config> element is found in xml, where 30 in the <session-timeout> element is the default time, measured in minutes, and its value can be modified. manual effectiveness: It is implemented by calling the invalidate () method and is mainly used for user logout. however, if you want to clear an object in the session, you can call the session. the removeAttribute (String key) method clears the specified object from the session, and the session is still valid.

Common Methods for session objects: <1> void setAttribute (String key, Object value) retains the Object to the session in the form of key/value

<2> the Object getAttribute (String key) uses the key to obtain the Object saved in the session.

<3> void invalidate (): sets the session object to be invalid.

<4> String getId () obtains the sessionid.

<5> void setMaxInactiveInterval (int interval): sets the session inactive time.

<6> int getMaxInactiveInterval (): obtains the effective activity time of the session, in seconds.

<7> void removeAttribute (String key): deletes the object corresponding to the specified name (key) from the session.

 

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.