JSP enables session management when the browser closes cookies

Source: Internet
Author: User

Generally, Session management uniquely identifies a user Session by storing the Session ID as a cookie in the user's Web browser. If the browser does not support cookies or sets the browser to reject cookies, we can rewrite the URL to implement session management.

In essence, URL rewriting adds parameters to a URL Connection and includes the session ID as the value in the connection. However, to make this take effect, you need to add the session ID for each connection in your servlet Response Section.

Adding session IDs to a connection can be simplified using one pair of Methods: response. encodeURL () enables the URL to contain the session ID. If you need to use redirection, you can use response. encodeRedirectURL () to encode the URL.

The encodeURL () and encodeRedirectedURL () methods first determine whether cookies are supported by the browser. If they are supported, the parameter URL is returned as is, and the session ID is maintained through cookies.

The following example shows two JSP files: hello1.jsp and hello2.jsp, and their influences. In hello1.jsp, we create a session and store an object instance in the session. Then you can click the connection on the page to reach hello2.jsp. In hello2.jsp, we obtain the originally placed object from the session and display its content. Note: In hello1.jsp, The encodeURL () method is called to obtain the link of hello2.jsp, so that when the browser disables cookies, the session ID is automatically added to the URL, and hello2.jsp can still obtain the session object.

First, use cookies. Disable the cookie support, restart the browser, and run it again. Each time you can see that session management is working and information can be transferred between pages.

Note: If you want this example to work in a browser with cookies disabled, 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 session is" + I. intValue ());
%>


Axeon sent at 11:05:38
Copyright©2000 www.jsper.com All rights reserved.
The catalog script used on this site was written by axeon
Website contact: [axeonOICQ: 1684897 ICQ: 75795533]

Related Article

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.