java servlet session

      No Comments on java servlet session

java servlet session

GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user’s information in the session before forwarding to home.jsp, which displays the user’s information like full name, address, etc.

java servlet session

Session is the most important features available on every server side scripting language. in java servlet session is the best way to store your data on server side. You can store user identification information on Serverside to identify the unique user. This example explains how we can store data on Session.

Jump to GTU WTAD Practical 19

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu18\

<html>
	<head>
		<title>GTU WTAD Practical 18</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as 
	request parameters. In case the authentication is successful, 
	it should setup a new session and store the user's information in the session before forwarding to home.jsp, 
	which displays the user's information like full name, address, etc. </h2>
	
	
		<form action="/gtu18/LoginVerificationUsingSession" method="get">
	
			<table>
				<tr><th colspan="2">Login</th></tr>
				<tr><td>Username : </td><td>  <input type="text" name="par1" value=""> </td></tr>
				<tr><td>Password : </td><td>  <input type="password" name="par2" value=""> </td></tr>
				<tr><td colspan="2"><input type="submit" value="Login"></td></tr>
			</table>http://adarshspatel.in/wp-admin/post.php?post=994&action=edit#
			
		</form>
	</body>
</html>

 

File Name : home.jsp

File Location : Tomcat_Apache\webapps\gtu18\

<html>
	<head>
		<title>GTU WTAD Practical 18</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc. </h2>
		<br><br><center>
		<table>
		<tr>
			<th>Welcome to Home Page</th>
		</tr>
		<tr>
			<td>Logged in user is : Admin</td>
		</tr>
		
		<tr>
			<td>Name: Adarsh Patel</td>
		</tr>
		
		<tr>
			<td>Address : Baroda</td>
		</tr>
		</table>
		</center>
		
	</body>
</html>

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu18\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 18  </display-name>
  <description>Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc. </description>
  
  <servlet>
    <servlet-name>LoginVerificationUsingSession</servlet-name>
    <servlet-class>LoginVerificationUsingSession</servlet-class>
  </servlet>

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

 

File Name : LoginVerificationUsingSession.java

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

/*
		GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc.
*/

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

public class LoginVerificationUsingSession extends HttpServlet
{
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");      
		out.println("<html><head><title>GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc. </title></head><body></h1></center><br><br>");
 
		//===============================================
		// Read parameter value send by page1
		//===============================================
		String s1 = request.getParameter("par1");
		String s2 = request.getParameter("par2");
		
		//===============================================
		// Read Current Session 
		//===============================================
		HttpSession current_session = request.getSession();

		//===============================================
		// Read Session Parameter 1 and 2
		//===============================================		
		String session_value1 = (String) current_session.getAttribute("session_par1");
		String session_value2 = (String) current_session.getAttribute("session_par2");
		
		if( s1.equals("admin") && s2.equals("admin") )
		{
			if( session_value1==null || session_value2==null)
			{
				//===============================================
				// Write Parameter 1 and 2 into Session
				//===============================================
				current_session.setAttribute("session_par1","admin");
				current_session.setAttribute("session_par2","admin");
			}
			
		}
		else
		{
			//===============================================
			// Invalid User redirect to login page
			//===============================================
			response.sendRedirect("index.jsp");
		}
		
	}
}

 

Output (index)

GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc.

Output (home)

GTU WTAD Practical 18 : Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc.

Leave a Reply