100% found this document useful (1 vote)
43 views13 pages

ITM University Gwalior: Academic Year 2021 - 25

1. The document describes a login application created using Java servlets and cookies. It includes HTML and Java files that implement login, logout, and profile functionality using cookies to maintain the user's session. 2. The HTML files include index.html, login.html, and link.html. The Java files include LoginServlet.java, LogoutServlet.java and ProfileServlet.java which handle the login, logout and profile functionality respectively using cookies. 3. The output shows different pages displayed based on login status - the front page, an error if trying to access profile without login, error on wrong login, welcome message on successful login, profile page content, and confirmation on logout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
43 views13 pages

ITM University Gwalior: Academic Year 2021 - 25

1. The document describes a login application created using Java servlets and cookies. It includes HTML and Java files that implement login, logout, and profile functionality using cookies to maintain the user's session. 2. The HTML files include index.html, login.html, and link.html. The Java files include LoginServlet.java, LogoutServlet.java and ProfileServlet.java which handle the login, logout and profile functionality respectively using cookies. 3. The output shows different pages displayed based on login status - the front page, an error if trying to access profile without login, error on wrong login, welcome message on successful login, profile page content, and confirmation on logout.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ITM UNIVERSITY GWALIOR

ACADEMIC YEAR 2021 -25

Department : B.Tech. (CSE)

PRACTICAL ASSIGNMENT - 3
Subject – Server Side Programming
Subject code – CSP0405

Submitted to:- Submitted by:-


Dr. Neeraj Goyal Sourabh Prajapati
(BETN 1CS21162)
Topic- Login App with Cookies
Directories-
1.
Index.html (html file)- save it in main folder

Input-

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title> Servlet Login Example</title

</head>

<body>

<h1> Welcome to Login App by Cookie</h1>

<a href="login.html">Login</a>

<a href="LogoutServlet">Logout</a>

<a href="ProfileServlet">Profile</a>

</body>

</html>

Output-
2.
Link.html (html file)- save this one too in main
folder

Input-
<a href="login.html">Login</a> <a href="LogoutServlet">Logout</a> <a
href="ProfileServlet">Profile</a> <hr>

Output-

3.Login.html(html file)- save this one too in main


folder
Input-
<form action="LoginServlet" method="post">

Name:<input type="text" name="name"><br>

Password:<input type="password" name="password">

<br>

<input type="submit" value="login">

</form>
3.

Output-

4. LoginServlet.java (java code file)- save it in


classes folder, make sure that it compiles
successfully and it’s class file is generated in that
same folder.
Input-

import java.io.IOException; import

java.io.PrintWriter;
4.
import jakarta.servlet.ServletException; import

jakarta.servlet.http.Cookie; import

jakarta.servlet.http.HttpServlet; import

jakarta.servlet.http.HttpServletRequest; import

jakarta.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException

response.setContentType("text/html"); PrintWriter

out=response.getWriter();

request.getRequestDispatcher("link.html").include(request, response);

String name=request.getParameter("name");
String password=request.getParameter("password");

if(password.equals("admin123"))

out.print("You are Succesfully logged in!");

out.print("<br>Welcome,"+name); Cookie

ck=new Cookie("name",name);

response.addCookie(ck);

} else

out.print("sorry, username or password error!");

request.getRequestDispatcher("login.html").include(request, response);

} out.close();

5. LogoutServlet.java (java code file)- save it in


classes folder, make sure that it compiles
successfully and it’s class file is generated in that
same folder.

Input-
import java.io.IOException; import

java.io.PrintWriter;
import jakarta.servlet.ServletException; import

jakarta.servlet.http.Cookie; import

jakarta.servlet.http.HttpServlet; import

jakarta.servlet.http.HttpServletRequest; import

jakarta.servlet.http.HttpServletResponse;

public class LogoutServlet extends HttpServlet

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException

response.setContentType("text/html"); PrintWriter

out=response.getWriter();

request.getRequestDispatcher("link.html").include(request, response);

Cookie ck=new Cookie("name","");

ck.setMaxAge(0);

response.addCookie(ck); out.print("you are

Succesfully logged out");

6. ProfileServlet.java (java code file)- save it in


classes folder, make sure that it compiles
successfully and it’s class file is generated in that
same folder.
Input-
import java.io.IOException; import

java.io.PrintWriter;

import jakarta.servlet.ServletException; import

jakarta.servlet.http.Cookie; import

jakarta.servlet.http.HttpServlet; import

jakarta.servlet.http.HttpServletRequest; import

jakarta.servlet.http.HttpServletResponse;

public class ProfileServlet extends HttpServlet

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException

response.setContentType("text/html"); PrintWriter

out=response.getWriter();

request.getRequestDispatcher("link.html").include(request,

response); Cookie ck[]=request.getCookies(); if(ck!=null)

String name=ck[0].getValue(); if(!name.equals("")||name!=null)

out.print("<b>Welcome to Profile</b>"); out.print("<b>Welcome,"+name);


}}

else

out.print("Please Login first");

request.getRequestDispatcher("login.html").include(request, response);

} out.close();

Output of the Program-


(1) Front page-

(2) You can’t visit profile page directly-


(3) If wrong password is entered-

(4) When you login “Succesfully”-


(5) Profile page-

(6) Logout Page-

You might also like