diff --git a/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java new file mode 100644 index 0000000..819f6c4 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/LoginServlet.java @@ -0,0 +1,51 @@ +package de.hsel.spm.baudas.web; + +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.Cookie; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +@WebServlet("/LoginServlet") + +public class LoginServlet extends HttpServlet { + + //private final String username = "admin"; + private final String password = "password"; + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // get request parameters for username and password + //String username = request.getParameter("username"); + String password = request.getParameter("pwd"); + + //if (this.username.equals(username) && this.password.equals(password)) { + if (this.password.equals(password)) { + //get the old session and invalidate + HttpSession oldSession = request.getSession(false); + if (oldSession != null) { + oldSession.invalidate(); + } + //generate a new session + HttpSession newSession = request.getSession(true); + + //setting session to expiry in 5 mins + newSession.setMaxInactiveInterval(5*60); + + Cookie message = new Cookie("message", "Welcome"); + response.addCookie(message); + response.sendRedirect("/index.jsp"); + } else { + RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html"); + PrintWriter out = response.getWriter(); + out.println("Das eingegebene Passwort ist falsch."); + rd.include(request, response); + } + } +} diff --git a/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java new file mode 100644 index 0000000..19a7852 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/web/LogoutServlet.java @@ -0,0 +1,27 @@ +package de.hsel.spm.baudas.web; + +import java.io.IOException; + +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; + +/** + * Servlet implementation class LogoutServlet + */ +@WebServlet("/web/LogoutServlet") +public class LogoutServlet extends HttpServlet { + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + //invalidate the session if exists + HttpSession session = request.getSession(false); + if(session != null){ + session.invalidate(); + } + response.sendRedirect(request.getContextPath() + "/loginPage.html"); + } +} diff --git a/src/main/webapp/preview/login.html b/src/main/webapp/preview/login.html index 8538999..4457556 100644 --- a/src/main/webapp/preview/login.html +++ b/src/main/webapp/preview/login.html @@ -23,15 +23,18 @@
- - lock_openLogin + +
+ + +
- +