Session management in the case of browser shutdown cookies

Source: Internet
Author: User
Tags integer session id
Cookie| Browser


If the browser does not support cookies, or if the browser is set to not accept cookies, we can use URL rewriting to implement session management.
Essentially URL overrides are by adding parameters to the URL connection and including the session ID as a value in the connection. However, for this to take effect, you need to add a session ID for each connection in your servlet response section. Adding a session ID to a connection can be simplified using a pair of methods: Response.encodeurl ()

Make the URL contain the session ID, and if you need to use redirection, you can use Response.encoderedirecturl () to encode the URL.
The Encodeurl () and Encoderedirectedurl () method first determines whether the cookie is supported by the browser, and if so, the parameter URL is returned as is, and the session ID is maintained via cookies.
Here's an example of two JSP files: hello1.jsp and hello2.jsp, and the impact between them. We simply create a session in the HELLO1.JSP and store an object instance in the sessions. The user can then click on the page's connection to reach hello2.jsp. In hello2.jsp, we get the previously placed object from the session and display its contents. Notice that we have called the Encodeurl () method in hello1.jsp to get the hello2.jsp link, so that the session ID is automatically added to the session when the browser deactivates cookies, and still gets the session Object.
The first is to run with cookies enabled. Then turn off support for cookies, restart the browser, and run again. Every time you see Session management at work, and you can pass information between pages.
Note that if you want this example to work in a browser that shuts down cookies, your JSP engine must support URL rewriting.

hello1.jsp

<%@ page session= "true"%>

<%

Integer num = new integer (100);

Session.putvalue ("num", num);

String URL =response.encodeurl ("hello2.jsp");

%>

<a href= ' <%=url%> ' >hello2.jsp</a>

hello2.jsp

<%@ page session= "true"%>

<%

Integer i= (integer) session.getvalue ("num");

Out.println ("Num value in the session is" +i.intvalue ());

%>




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.