Introduction to Servlet technology
Chapters
Working with session
Servlets - Session Tracking
HTTP is a "stateless" protocol meaning when client retrieves a Web page, client opens a new separate connection to Web server and it does not keep any record of previous client request.
The HttpSession Object
The servlet container have interface for creating session between HTTP client and HTTP server. The session persists for specified time period across more than one connection or page request from user.
We can access HttpSession
object by calling public method getSession()
of HttpServletRequest
as below:
HttpSession session = request.getSession();
Below are the few methods used for accessing the session objects :
Method & Description |
public Object getAttribute(String name)
|
public Enumeration getAttributeNames()
|
public long getCreationTime()
|
public String getId()
|
public long getLastAccessedTime()
|
public int getMaxInactiveInterval()
|
public void invalidate()
|
public boolean isNew()
|
public void removeAttribute(String name)
|
public void setAttribute(String name, Object value)
|
public void setMaxInactiveInterval(int interval)
|
public Object getAttribute(String name)
|
public Enumeration getAttributeNames()
|
public long getCreationTime()
|
public String getId()
|
public long getLastAccessedTime()
|
public int getMaxInactiveInterval()
|
Below is the example of managing session in servlets.
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); // print session info Date created = new Date(session.getCreationTime()); Date accessed = new Date(session.getLastAccessedTime()); out.println("ID " + session.getId()); out.println("Created: " + created); out.println("Last Accessed: " + accessed); } }
Here is what we get:
Deleting Session Data
The Session data can be removed as shown below:
- Remove particular attribute: public void removeAttribute(String name) method used to remove session.
- Delete whole session: public void invalidate() method can be used to delete entire session.
- Setting Session timeout: public void setMaxInactiveInterval(int interval) method will set timeout to session individually.
- web.xml Configuration: session time out can be set in web.xml as follows.
<session-config> <session-timeout>15</session-timeout> </session-config>
In the next part of this quick introduction guide we will explore how to connect to a database.
Description
This guide introduces Servlet technology and we will cover below topics
- What is a Servlet?
- Initial Setup
- Life Cycle
- Various Examples
- Client Request
- Server Response
- Http Status Codes
- Writing Filters
- Exception Handling
- Cookies Handling
- Session Tracking
- Database Access
- Packaging
- Internationalization
This is to the point introduction to the topic to get you started
Prerequisites
Working knowledge of Core Java is essential
Audience
Beginner to Java Servlet technology or students looking to brush up their skills quickly
Learning Objectives
Learn Java Servlets
Author: Subject Coach
Added on: 2nd May 2015
You must be logged in as Student to ask a Question.
None just yet!