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]