TYCOA061
Assignment 3:
Login.jsp :-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<section class="vh-100" style="background-color: #508bfc;">
<div class="container py-5 h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-12 col-md-8 col-lg-6 col-xl-5">
<div class="card shadow-2-strong" style="border-radius: 1rem;">
<div class="card-body p-5 text-center">
<h3 class="mb-5">Enter Login Details</h3>
<form action="login" method="post">
<div class="form-outline mb-4">
<input type="text" id="typeEmailX-2" class="form-
control form-control-lg" name="usName" />
<label class="form-label" for="typeEmailX-
2">UserName</label>
</div>
<div class="form-outline mb-4">
<input type="password" id="typePasswordX-2"
class="form-control form-control-lg" name="usPass" />
<label class="form-label" for="typePasswordX-
2">Password</label>
</div>
<div class="form-check d-flex justify-content-start
mb-4">
<input class="form-check-input" type="checkbox"
value="" id="form1Example3" />
<label class="form-check-label"
for="form1Example3"> Remember password </label>
</div>
<button class="btn btn-primary btn-lg btn-block"
type="submit">Login</button>
</form>
<hr class="my-4">
</div>
</div>
</div>
</div>
</div>
</section>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/
[email protected]/dist/umd/popper.min.js"></scrip
t>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
LoginServlet.java :-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
// doPost() method
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
// Set the content type of response to "text/html"
response.setContentType("text/html");
// Get the print writer object to write into the response
PrintWriter out = response.getWriter();
// Get the session object
HttpSession session = request.getSession();
// Get User entered details from the request using request
parameter.
String user = request.getParameter("usName");
String password = request.getParameter("usPass");
// Validate the password - If password is correct,
// set the user in this session
// and redirect to welcome page
if (password.equals("admin")) {
session.setAttribute("user", user);
response.sendRedirect("welcome.jsp?name=" + user);
}
// If the password is wrong, display the error message on the login
page.
else {
RequestDispatcher rd =
request.getRequestDispatcher("login.jsp");
out.println("<font color=red>Password is wrong.</font>");
rd.include(request, response);
}
// Close the print writer object.
out.close();
}
}
LogoutServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogoutServlet() {
super();
}
// doGet() method
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
// Get the print writer object to write into the response
PrintWriter out = response.getWriter();
// Set the content type of response to "text/html"
response.setContentType("text/html");
// For understanding purpose, print the session object in the console
before
// invalidating the session.
System.out.println("Session before invalidate: " +
request.getSession(false));
// Invalidate the session.
request.getSession(false).invalidate();
// Print the session object in the console after invalidating the
session.
System.out.println("Session after invalidate: " +
request.getSession(false));
// Apply styling to the logout message
out.println("<html>");
out.println("<head>");
out.println("<title>Logout Page</title>");
out.println("<style>");
out.println("body {");
out.println(" font-family: Arial, sans-serif;");
out.println(" background-color: #f5f5f5;");
out.println(" text-align: center;");
out.println(" padding: 50px;");
out.println("}");
out.println("h1 {");
out.println(" color: #333;");
out.println("}");
out.println("</style>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Thank you! You are successfully logged out.</h1>");
out.println("</body>");
out.println("</html>");
// Close the print writer object.
out.close();
}
}
OUTPUT: