JSP implementation session management in the browser closes cookies

xiaoxiao2021-03-06  90

Typically, the session management is to uniquely identify each user session in a user's web browser through the server as a cookie in a user's web browser. If the browser does not support cookies, or set the browser to not accept cookies, we can rewrite through the URL to implement session management. Substantially URL rewrite is to add parameters to the URL connection and contain the session ID as a value in the connection. However, in order to take effect, you need to add a session ID for each connection of your servlet response section. Putting the session ID to a connection to simplify: response.EncodeURL () enables the URL contains the session ID, if you need to use the redirection, you can use Response.EncodeRedirectURL () to encode the URL. EncodeURL () and EncoderedIrectedURL () method First determine if the cookies are supported by the browser; if supported, the parameter URL returns, the session ID will be maintained by cookies. Look at the examples below, two JSP files: hello1.jsp and hello2.jsp, and the impacts between them. We simply create a session in Hello1.jsp, and store an object instance in the session. The user can then click the connection to the page to reach Hello2.jsp. In Hello2.jsp, we get the original object that is placed and display it from the session. Note that we call the EncodeURL () method in Hello1.jsp to get the link of Hello2.jsp, so that if the browser disables cookies, the session ID is automatically added to the URL, and hello2.jsp can also get the session object. First run in the case of enable cookies. Then close the support for cookie, restart the browser, and run once. Every time you can see that session management works, and can pass information between pages. Note that if you want this example to work in a browser that closes the cookies, your JSP engine must support URL rewrite. Hello1.jsp <% @ Page session = "true"%> <% integer Num = new integer (100); session.putValue ("num", num); string url = response.encodeurl ("hello2.jsp"); %>