GTU WTAD Practical 25 : java servlet session example

GTU WTAD Practical 25 : Develop an application to keep track of one user across several servlet invocations within the same browser session.

GTU WTAD Practical 25

This Servlet Example Explains how to use Servlet Session to manage Session between many pages of Servlet. On First Visit of user it will display home page with form, once user login it will be redirect to home page. If user tries to open login page from the same browser and different/same tab it will directly redirect to home as user has already logged in and session has been created, if user clicks on signout then session will be invalidated and you need to login again to visit home page.

Jump to GTU WTAD Pracitcal 26

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu25\

<%
		String session_value = (String)session.getAttribute("user");
		if (session_value != null) 
		{ 
			response.sendRedirect("./Welcome");
		}
%>
<html>
	<head>
		<title>GTU WTAD Practical 25</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 25 : Develop an application to keep track of one user across several servlet invocations within the same browser session. </h2>

		<table>
		<tr>
			<th>Session Tracking </th>
		</tr>
		<tr>
		<td>
			<form action="./Welcome" method="post">
				Username : <input type="text" name="u"> <br>
				Password : <input type="password" name="p"> <br>
				<input type="submit" value="Login">
			</form>
			<br><br>
		</td>
		</tr>
		</table>
	</body>
</html>

 

File Name : Welcome.java

File Location : Tomcat_Apache\webapps\gtu25\WEB-INF\classes

/*
		GTU WTAD Practical 25 :  Develop an application to keep track of one user across several servlet invocations within the same browser session. 
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Welcome extends HttpServlet
{

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
	{
		doPost(request,response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
	{
		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GTU WTAD Practical 25 :  Develop an application to keep track of one user across several servlet invocations within the same browser session.  </title></head><body><h2>GTU WTAD Practical 25 :  Develop an application to keep track of one user across several servlet invocations within the same browser session.  </h2>");
		
		out.println("<center> <a href=\"./Signout\"> Signout </a> </center> ");
		
		String u = request.getParameter("u");
		String p = request.getParameter("p");
		
		HttpSession session = request.getSession();
		String session_value = (String)session.getAttribute("user");
		if (session_value == null) 
		{ 
			// No Session found.. Need to create new session
			
			session.setAttribute("user", u);
			out.println("Welcome " + u + " for First Time");
		}
		else
		{
			//Session Found
			out.println("Welcome Back " + session_value);
		}
		
		
		out.println("</body></html>");
	}

}

 

File Name : Signout.java

File Location : Tomcat_Apache\webapps\gtu25\WEB-INF\classes

/*
		GTU WTAD Practical 25 :  Develop an application to keep track of one user across several servlet invocations within the same browser session. 
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class Signout extends HttpServlet
{

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
	{
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("./index.jsp");
	}

}

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu25\WEB-INF\

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 25  </display-name>
  <description>  Develop an application to keep track of one user across several servlet invocations within the same browser session.  </description>
  
  <servlet>
	<servlet-name>Welcome</servlet-name>
	<servlet-class>Welcome</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>Welcome</servlet-name>
	<url-pattern>/Welcome</url-pattern>
  </servlet-mapping>
  
   <servlet>
	<servlet-name>Signout</servlet-name>
	<servlet-class>Signout</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>Signout</servlet-name>
	<url-pattern>/Signout</url-pattern>
  </servlet-mapping>
    
</web-app>

 

Output

GTU WTAD Practical 25

Output

GTU WTAD Practical 25

Output

GTU WTAD Practical 25

Leave a Reply