This commit is contained in:
edgar 2019-05-20 17:08:47 +02:00
parent ce5e3a1f27
commit ba1ee2d06c
3 changed files with 84 additions and 3 deletions

View File

@ -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("<font color=red>Das eingegebene Passwort ist falsch.</font>");
rd.include(request, response);
}
}
}

View File

@ -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");
}
}

View File

@ -23,15 +23,18 @@
<div class="divider"></div>
<div class="row">
<div class="col s8 push-s2">
<input id="password" type="password" class="validate" placeholder="Passwort">
<a class="waves-effect waves-light btn blue-grey lighten-2"><i class="material-icons left">lock_open</i>Login</a>
</div>
</div>
</div>
</div>
</div>
</div>
<form action="LoginServlet" method="post">
<input id="password" type="password" class="validate" placeholder="Passwort">
<input class="white-text btn blue-grey lighten-2" type="submit" value="Login">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!--<a class="waves-effect waves-light btn blue-grey lighten-2"><i class="material-icons left">lock_open</i>Login</a>-->
<!--Anfang Skriptbereich-->
<!--Script für Materlialize-->