index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center><form action="LoginServlet" method="post">
<table>
<tr>
<th>UserName:</th><th><input type="text" name="username"/></th>
</tr>
<tr>
<th>Password:</th><th><input type="password" name="password"/></th>
</tr>
<tr>
<th></th>
<th><input type="submit" value="login"/></th>
</tr>
</table>
</form> </center>
</body>
</html>
2. Home.jsp
<%--
Document : Home
Created on : Apr 8, 2020, 5:13:44 PM
Author : Sahaj
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<Center>
Welcome <%=session.getAttribute("user")%>
</br></br> Home Page (Login Success)
</br></br> <a href="./ProfileServlet">My Profile</a>
</br></br> <a href="./LogoutServlet">Logout</a>
</Center>
</body>
</html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center><form action="LoginServlet" method="post">
<table>
<tr>
<th>UserName:</th><th><input type="text" name="username"/></th>
</tr>
<tr>
<th>Password:</th><th><input type="password" name="password"/></th>
</tr>
<tr>
<th></th>
<th><input type="submit" value="login"/></th>
</tr>
</table>
</form> </center>
</body>
</html>
2. Home.jsp
<%--
Document : Home
Created on : Apr 8, 2020, 5:13:44 PM
Author : Sahaj
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<Center>
Welcome <%=session.getAttribute("user")%>
</br></br> Home Page (Login Success)
</br></br> <a href="./ProfileServlet">My Profile</a>
</br></br> <a href="./LogoutServlet">Logout</a>
</Center>
</body>
</html>
3.ProfilePage.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
This is Profile Page
</center>
</body>
</html>
4.LoginServlet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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;
import javax.servlet.http.HttpSession;
/**
*
* @author Sahaj
*/
@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// accept username and password from index.html file
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("abc")&&password.equals("123")){
//here set static username and password if user abc and pass 123 than go to home page with session value
HttpSession session = request.getSession();
//get username from login username value and set it to session attribute
String name = request.getParameter("username");
session.setAttribute("user", name);
response.sendRedirect("Home.jsp");
}else{
//wrong username and password
out.println("wrong username and password...");
}
}
}
5 .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;
import javax.servlet.http.HttpSession;
/**
*
* @author Sahaj
*/
@WebServlet(urlPatterns = {"/LogoutServlet"})
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//get current session
HttpSession session = request.getSession();
//clear
session.invalidate();
//than go to login page
response.sendRedirect("index.html");
}
}
6.ProfileServlet.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;
import javax.servlet.http.HttpSession;
/**
*
* @author Sahaj
*/
@WebServlet(urlPatterns = {"/ProfileServlet"})
public class ProfileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//getsession
HttpSession session = request.getSession();
//check if session is null or not if null than redirect to login
if(session.getAttribute("user")!=null){
//if session not null than go to profilepage
response.sendRedirect("ProfilePage.html");
}else{
// is session attribute is null than redirect to login page
response.sendRedirect("index.html");
}
}
}
No comments:
Post a Comment