cookie|cookies|js| Browser
Typically, session management uniquely identifies each user session by storing it in the user's Web browser as a cookie through the server. 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. The
essentially URL rewrite is 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 by using a pair of methods: Response.encodeurl () so that the URL contains the session ID, and if you need to use redirects, you can use the Response.encoderedirect URL () 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.
Look at the following example, two JSP files: hello1.jsp and hello2.jsp, and their effects. 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
is first 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 closes cookies, your JSP engine must support URL rewriting.
hello1.jsp
& Lt;%@ 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 ());
%>
Axeon sent
copyright©2000 www.jsper.com All rights reserved at 2000-08-03 11:05:38.
The catalog script used on this site is written by Axeon
Site Contact: [axeonoicq:1684897 icq:75795533]