GTU WTAD Practical 17 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid

GTU WTAD Practical 17 :  Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid

GTU WTAD Practical 17 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid

This program explains use of cookies to store user information at client side. Cookies can be used to store user related preferences or to improve user interface or to display proper advertisement based on user interest. This program explains how we can write data into Cookies and how we can read Cookies.

Jump to GTU WTAD Practical 18

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu17\

<html>
	<head>
		<title>GTU WTAD Practical 17</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 17 :  Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </h2>
	
	
		<form action="/gtu17/LoginVerificationUsingCookies" 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>
			
		</form>
	</body>
</html>

File Name : home.jsp

File Location : Tomcat_Apache\webapps\gtu17\

<html>
	<head>
		<title>GTU WTAD Practical 17</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 17 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </h2>
	
	
		<br><br><center>
		<table>
		<tr>
			<th>Welcome to Home Page</th>
		</tr>
		</table>
		</center>
		
	</body>
</html>

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu17\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 17  </display-name>
  <description>ADevelop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </description>
  
  <servlet>
    <servlet-name>LoginVerificationUsingCookies</servlet-name>
    <servlet-class>LoginVerificationUsingCookies</servlet-class>
  </servlet>

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

 

File Name : LoginVerificationUsingCookies.java

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

/*
		GTU WTAD Practical 17 :  Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid
*/

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;


public class LoginVerificationUsingCookies 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 17 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid </title></head><body></h1></center><br><br>");
 
		//===============================================
		// Read parameter value send by page1
		//===============================================
		String s1 = request.getParameter("par1");
		String s2 = request.getParameter("par2");
		
		//===============================================
		// Read all cookie from Browser
		//===============================================
		Cookie[] c = request.getCookies();
		
		boolean user=false,pass=false;
			
		for(int i=0;i<c.length;i++)
		{
			Cookie t = c[i];
			//===============================================
			// Verify username and setup flag
			//===============================================
			if(t.getName().equals("username") && t.getValue().equals(s1) )
			{
				user=true;
			}
			
			//===============================================
			// Verify password and setup flag
			//===============================================
			if(t.getName().equals("password") && t.getValue().equals(s2) )
			{
				pass=true;
			}
			
		}
		
		if(user==true && pass==true)
		{
			//===============================================
			// Cookie Validated redirect user to home page
			//===============================================
			response.sendRedirect("home.jsp");
		}
		else
		{
		
			//===============================================
			//Create Cookie and Write into Browser....
			//===============================================
			Cookie c1 = new Cookie("username",s1);
			Cookie c2 = new Cookie("password",s2);
			
			//===============================================
			//Setting Maximum Lifetime of the Cookie
			//===============================================
			c1.setMaxAge(60*60*24*7);
			c2.setMaxAge(60*60*24*7);
			
			//===============================================
			// Write Cookie into Browser
			//===============================================
			response.addCookie(c1);
			response.addCookie(c2);
			
			//Redirect Back to Home Page as Cookies was not exist
			response.sendRedirect("index.jsp");
		}
	
		
	}
}

 

Output (index page)

GTU WTAD Practical 17 : Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are nto valid

Output (home page after login)

GTU_17_2

Leave a Reply