Created
February 23, 2016 13:17
-
-
Save anonymous/c5fbf35cfa91571a1bb2 to your computer and use it in GitHub Desktop.
Web App code for stackoverflow question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import java.io.IOException; | |
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; | |
@WebServlet({"/home" }) | |
public class ContentServlet extends HttpServlet{ | |
private static final long serialVersionUID = -2907124992996374890L; | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ | |
if(!SessionValidator.isSessionValid(request, response)){ | |
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp"); | |
dispatcher.forward(request, response); | |
return; | |
} | |
RequestDispatcher dispatcher = request.getRequestDispatcher("home.jsp"); | |
dispatcher.forward(request, response); | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ | |
doGet(request, response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Home</title> | |
</head> | |
<body> | |
<% | |
String userId = (String) session.getAttribute("userId"); | |
%> | |
<h1>You successfully logged in!!</h1> | |
<h2>Welcome <%=userId%></h2> | |
<a href="login?action=logout">logout</a> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Login</title> | |
</head> | |
<body> | |
<% | |
String message = (String) request.getAttribute("message"); | |
message = message==null ? "" : message; | |
%> | |
<form action="login" method="post"> | |
<h3><%=message %></h3> | |
<label>Id: </label> <input type="text" name="userId"> | |
<label>Password: </label> <input type="password" name="password"> | |
<input type="submit" value="Login"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import java.io.IOException; | |
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 = 6893524305259610055L; | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
String action = request.getParameter("action"); | |
if("logout".equals(action)){ | |
HttpSession session = request.getSession(false); | |
if(!SessionValidator.isSessionValid(request, response)){ | |
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp"); | |
dispatcher.forward(request, response); | |
return; | |
} | |
session.invalidate(); | |
} | |
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp"); | |
dispatcher.forward(request, response); | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
String userId = request.getParameter("userId"); | |
String password = request.getParameter("password"); | |
if("admin".equals(userId) && "pass1234".equals(password)){ | |
HttpSession session = request.getSession(); | |
session.invalidate(); | |
session = request.getSession(true); | |
session.setAttribute("userId", userId); | |
response.sendRedirect("home"); | |
} | |
else{ | |
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp"); | |
request.setAttribute("message", "Invalid Login ID / password"); | |
dispatcher.forward(request, response); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
public class SessionValidator { | |
public static boolean isSessionValid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ | |
HttpSession session = request.getSession(false); | |
if(session==null || session.getAttribute("userId")==null){ | |
request.setAttribute("message", "Session expired, login again"); | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
<display-name>mywebapp</display-name> | |
<session-config> | |
<session-timeout>5</session-timeout> | |
</session-config> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment