CSS font stacks, developer FAQs & web standards

Your banner ad here

WestNIC provides reliable web hosting services

Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.

Session management

Q: What is the use of sessions in servlets?

A: The servlet HttpSession interface is used to simulate the concept that a person's visit to a Web site is one continuous series of interactions. This is often the case, but the HTTP protocol is basically a request-response mechanism with no necessary connection between one request and the next.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What is the use of sessions in servlets?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Are sessions created on the server side?

A: A servlet session is created and stored on the server side. The servlet container keeps track of all the sessions it manages and fulfills servlet API requests to get HttpSessions, manipulate object data stored with them and trigger event callbacks.

To the maintain the session, Web clients must pass back a valid session identifier as a cookie header value or dynamically generated URL parameter. In this sense, the session is also stored by the client, but only as a token reference.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Can I create a session with GenericServlet?

A: There are no protocol-specific features in GenericServlet, which is an implementation of the basic, general purpose Servlet interface. Servlet-based sessions are designed only for interactions using the HTTP protocol, which has two key features necessary for a servlet container to simulate continuous user sessions: cookies and URL-based navigation, which supports URL-rewriting. The servlet API therefore places the HttpSession interface in the javax.servlet.http package, and session references are only available through classes in this package.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I assemble data from multiple input forms?

A: First, it is best to use a single servlet to handle each form submission. A single servlet for all input would be too complicated. Give each servlet responsibility to validate a single form input, and pass the error cases on to JSP documents that explain the problem and allow users to amend the input.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I assemble data from multiple input forms?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I clean up object references when a session ends?

A: In normal service conditions a servlet session may expire when the application user exceeds the maximum inactive time interval for the session, or the application calls the session's invalidate() method in response to a "log out" type request. In both cases, the session ID reference is invalidated so any further requests for the session will return null, then all the object references associated with the session are unbound and made available for garbage collection (if no other references remain).

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I clean up object references when a session ends?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I recover an expired session?

A: If a session has expired, it means a browser has made a new request that carries a session identifier, such as a cookie entry, for which the servlet container has no record. Servlet containers usually discard sessions after a standard time-out period, so they do not have to maintain sessions indefinitely. If the session has expired on the server side, there is no way to re-construct it, because no reference remains.

It is possible to request sessions live longer using the HttpSession setMaxInactiveInterval(int) method. Many servlet containers also support persistent sessions, which are stored between separate invocations of the container, but configuration details vary.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I change the session ID of the page?

A: The servlet session represents a relationship between the servlet container and the end user's browser over a number of request-response cycles, it is not bound to a particular page. Generally, you should not attempt to change users' session ID values because it is very likely to result in the loss of the session or with people acquiring other users' data. The session ID value should be managed by the servlet container.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Cookie controls

Q: What are persistent cookies and pre-session cookies?

A: Cookies are relatively simple information stores that may be maintained by a Web browser using data sent in the response header from a Web server. There is no obligation for a Web browser to accept the cookie or maintain the information it is sent. Some people disable cookies in their browser settings and you should design your application with this possibility in mind.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
What are persistent cookies and pre-session cookies?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I disable a cookie?

A: The storage of cookies is controlled by their maximum age property. A positive value means the cookie should be stored for the given number of milliseconds from the present time. A negative value means the cookie should not be stored and will expire when the browser is shut down. To delete an existing cookie, call setMaxAge(0) before issuing the servlet response. Use the code below to delete all cookies for the current site, for example:

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How do I disable a cookie?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Other data handling techniques

Q: How can I pass values between JSPs without using sessions?

A: There are four main alternatives to full session-based transfer of data values, two of them use features of the session tracking API: cookies and URL-rewriting, detailed below.

premium content omitted

Sign up for premium content now Access all premium content for $50: sign-up now.
How can I pass values between JSPs without using sessions?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning, please read

Home · CSS · Java · Javascript · HTML · Help · Log