4.2 Create a web form which processes servlet and demonstrates use of cookies and sessions.

login.html

<html>
<head>
<title> Login Page </title>
</head>

<body>
<form action="loginservlet" method="post">
<table>
<tr>
<td>User Name: </td>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<td>Password: </td>
<td> <input type="password" name="pass"> </td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" name="submit" value="SUBMIT"> </td>
</tr>
</form>
</body>
</html>

loginservlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginservlet extends HttpServlet
{
protected void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("name");
String password=req.getParameter("pass");
if(name.equals("Mayur") && password.equals("Thakkar")) {
HttpSession session=req.getSession();
session.setAttribute("Name", name);
out.println("<br/><h1>Welcome :"+ name + "</h1> "); out.println("<br/> <a href=profile> Click here </a>");
}
else{
out.println("<h1>You Have entered Wrong Password </h1>"); out.println("<br/> <a href=login.html> Click here </a> to Login"); }
}
}

Profile.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class profile extends HttpServlet
{
protected void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession(false);
if(session!=null) {
String name=(String) session.getAttribute("Name"); out.println("Welcome," + name);
out.println("<h1>This is Session Program. </h1> ");
out.println("<a href=logoutservlet> Click here to Logout </a>");
}
else {
out.println("<br> <h1> Plz Login First</h1>");
out.println("<br/> <a href=login.html> Click here </a> to Login");
}
}
}

Logoutservlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logoutServlet extends HttpServlet
{
public void doGet (HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession();
session.invalidate();
out.println("<br> <h1>You have successfully Logout </h1>"); out.println("<br/> <a href=login.html> Click here </a> to Login"); }
}

Web.xml
<servlet>
<servlet-name>loginservlet</servlet-name> <servlet-class>loginservlet</servlet-class>
</servlet>

<servlet>
<servlet-name>logoutservlet</servlet-name> <servlet-class>logoutServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>profile</servlet-name> <servlet-class>profile</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>profile</servlet-name> <url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>logoutservlet</servlet-name> <url-pattern>/logoutservlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>loginservlet</servlet-name> <url-pattern>/loginservlet</url-pattern>
</servlet-mapping>

Happy Coding :)