Thursday 28 April 2011

14) Simple Project to check session exist or not and also to return only preexisting session

In this project we:
1)check whether the session already existed or was just created. (MySession.java)
2)ask for ONLY a pre-existing session. (MySessionChk.java)

To run this project we need following files:
1)web.xml
2)index.jsp
3)MySession.java
4)MySessionChk.java



1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
  <servlet>
       <servlet-name>Session Ex1</servlet-name>
       <servlet-class>com.jexamples.web.MySession</servlet-class>
  </servlet>
  <servlet-mapping>
       <servlet-name>Session Ex1</servlet-name>
       <url-pattern>/SessionTest.do</url-pattern>
  </servlet-mapping>
  <servlet>
       <servlet-name>Session Ex2</servlet-name>
       <servlet-class>com.jexamples.web.MySessionChk</servlet-class>
  </servlet>
  <servlet-mapping>
       <servlet-name>Session Ex2</servlet-name>
       <url-pattern>/SessionTestChk.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


2)index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body bgcolor="skyblue">
    <h3> <a href="SessionTest.do"> click to check whether the session already existed or was just created </a></h3>
    <h3> <a href="SessionTestChk.do"> click here if you want ONLY a pre-existing session </a></h3>
  </body>
</html>


3)MySession.java

package com.jexamples.web;

import java.io.IOException;
import java.io.PrintWriter;


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


public class MySession extends HttpServlet
{
  private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
  {
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 out.println("<body bgcolor='blue'><h1> WELCOME!!!!<h1> ");
 
 HttpSession session = request.getSession();
 
 String sId;
 if(session.isNew())
 {
 sId = session.getId();
 out.println("<h2>Session Id is = "+sId+"</h2>");
 out.println("<h2> This is a new session </h2>");
 }
 else
 {
 sId = session.getId();
 out.println("<h2>Session Id is = "+sId+"</h2>");
 out.println("<h2>Welcome back</h2>");
 }
 out.println("</body>");
 
  }
}


4)MySessionChk.java

package com.jexamples.web;

import java.io.IOException;
import java.io.PrintWriter;


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


public class MySessionChk extends HttpServlet
{
  private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
  {
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 out.println("<body bgcolor='blue'><h1> WELCOME!!!!<h1> ");
 
 HttpSession session = request.getSession(false);
 
 String sId;
 if(session==null)
 {   
 out.println("<h2> No session was available</h2>");
 out.println("<h2> making new one .. </h2>");
 session = request.getSession();
 sId = session.getId();
 out.println("<h2>Session Id is = "+sId+"</h2>");
 
 }
 else
 {   out.println("<h2>There was a session</h2>");
 sId = session.getId();
 out.println("<h2>Session Id is = "+sId+"</h2>");
 out.println("<h2>Welcome back</h2>");
 }
 out.println("</body>");
 
  }
}



No comments:

Post a Comment