0% found this document useful (0 votes)
9 views3 pages

Servlet-Based Login Application Guide

The document outlines the development of a Servlet-based login application for session tracking, consisting of an HTML login form, a Login Servlet for user authentication, a Welcome Servlet to display a welcome message, and a Logout Servlet to invalidate the session. The Login Servlet checks credentials and creates a session if valid, while the Welcome Servlet retrieves session information to greet the user. The Logout Servlet allows users to log out and invalidates their session.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Servlet-Based Login Application Guide

The document outlines the development of a Servlet-based login application for session tracking, consisting of an HTML login form, a Login Servlet for user authentication, a Welcome Servlet to display a welcome message, and a Logout Servlet to invalidate the session. The Login Servlet checks credentials and creates a session if valid, while the Welcome Servlet retrieves session information to greet the user. The Logout Servlet allows users to log out and invalidates their session.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

3.

Develop Servlet based login application for session Tracking

1. HTML Login Form – Collects username and password.


2. Login Servlet – Authenticates user and creates session.
3. Welcome Servlet – Displays welcome message if session exists.
4. Logout Servlet – Invalidates session.

1. HTML Login Form


<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login Page</h2>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
[Link] Servlet
import [Link].*;
import [Link].*;
import [Link].*;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = [Link]("username");
String password = [Link]("password");
if ("admin".equals(username) && "admin123".equals(password)) {
HttpSession session = [Link]();
[Link]("user", username);
[Link]("WelcomeServlet");
} else {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h3>Invalid credentials. Try again.</h3>");
[Link]("<a href='[Link]'>Back to Login</a>");
}
}
}
[Link] Servlet
import [Link].*;
import [Link].*;
import [Link].*;
public class WelcomeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = [Link](false); // Don't create new session
[Link]("text/html");
PrintWriter out = [Link]();
if (session != null && [Link]("user") != null) {
String user = (String) [Link]("user");
[Link]("<h2>Welcome, " + user + "!</h2>");
[Link]("<a href='LogoutServlet'>Logout</a>");
} else {
[Link]("<h3>Please login first.</h3>");
[Link]("<a href='[Link]'>Login</a>");
}
}
}
[Link] Servlet
import [Link].*;
import [Link].*;
import [Link].*;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = [Link](false);
if (session != null) {
[Link]();
}
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h3>You have been logged out.</h3>");
[Link]("<a href='[Link]'>Login Again</a>");
}
}

You might also like