Share on Google+Share on Google+

Get Session Id

In this tutorial we want to describe you a code that helps you in understanding Get Session Id .

Get Session Id


In this tutorial we want to describe you a code that helps you in understanding Get Session Id .In this example we import a package javax.servlet.http.HttpSession,this provide an object such that servlet can track, store and retrieve the information of the user. The servlet include the class Get Session Id,Inside this class, process request  method include   -

1)Request.getsession(true)   -  This method returns you the current session linked  with the user making the request. In this case true is a Boolean specify that if the user has no current valid session, this method creates one or returns null if create is false.

2)session.getId ( )  -  This method return you the current session Id.

3)response.setContentType ( "text/html")  - This is response object send  to the browser rendered the output content or MIME type in text or HTML Page.

4)response.getwriter ( )   - The get Writer ( )method is a response object returns you a print writer that is used to write data  sent to the client.

The try block include the print writer object that print a HTML page

5)out.close ( )   -   This method is used to close the print writer ,that causes the data written to the stream to be returned to the client.

The session id  transmitted back to the response object so that whenever the client makes any request then it should also attach the session id with the request object.

The service method do get, do post method is used  in servlet class  that provides a service to a client. The do get, do post process the response to the client based on client request information.

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetSessionId extends HttpServlet {

  protected void processRequest(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException, IOException {

  HttpSession session = request.getSession(true);
  String sessionId = session.getId();

  PrintWriter out = response.getWriter();
  try {
  out.println("<title>Servlet GetSessionId</title>");
  out.println("<h2>Session Id : " + sessionId+"</h2>");

  finally {

  protected void doGet(HttpServletRequest request, 
   HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);

  protected void doPost(HttpServletRequest request, 
   HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);


Download code



Posted on: November 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Get Session Id  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image