For few days I am tryng to fix a problem in a tiny servlet/jsp application.
I have 2 JSP files, index.jsp and adminPage.jsp. First one with a login form, mail and password, a button to submit the form to login servlet and a button that redirects to adminPage servlet that calls the adminpage.jsp if the login action is successfull. Login action works well, same for the transition to adminPage servlet and the forward to adminPage.jsp.
Thep roblem is when the submit button from adminPage.jsp is pressed and it submit to login servlet one of the 3 attributes from session is removed from it. So when it get in login servlet that attribute is null. The attribute that just disapears is "BackEndNames.ACCOUNT". I do not remove it in adminPage servlet.
Servlets
//login
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Account account;
UserDetails userDetails;
HttpSession httpSession = request.getSession();
LOG.debug("httpSession ID: " + httpSession.getId());
LOG.debug("is httpSession new: " + httpSession.isNew());
Enumeration<String> sessionEnum = httpSession.getAttributeNames();
while(sessionEnum.hasMoreElements()){
LOG.debug("SESSION PARAM NAME: " + sessionEnum.nextElement());
}
try{
if(httpSession.getAttribute(BackEndNames.ACCOUNT) == null){
LOG.debug("ACCOUNT FROM SESSION: " + httpSession.getAttribute(BackEndNames.ACCOUNT));
account = login(request);
// put account and login state on the session
httpSession.setAttribute(BackEndNames.ACCOUNT, account);
LOG.debug("ACCOUNT SET ON SESSION: " + httpSession.getAttribute(BackEndNames.ACCOUNT));
}else{
account = (Account)httpSession.getAttribute(BackEndNames.ACCOUNT);
}
if(httpSession.getAttribute(BackEndNames.USER_DETAILS) == null){
userDetails = getUserDetailsForAccount(account);
httpSession.setAttribute(BackEndNames.USER_DETAILS, userDetails);
}else {
userDetails = (UserDetails)httpSession.getAttribute(BackEndNames.USER_DETAILS);
}
request.setAttribute(BackEndNames.LOGIN_STATUS, true);
httpSession.setAttribute(BackEndNames.LOGIN_STATUS, true);
LOG.debug("LOGIN SUCCESSFULY");
// set account and the login state for the jsp
request.setAttribute(BackEndNames.ACCOUNT, account);
request.setAttribute(BackEndNames.USER_DETAILS, userDetails);
//redirect to index.jsp with the above data
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}catch (Exception e){
LOG.debug("EXCEPTION !!! " + e.getMessage() );
e.printStackTrace();
request.setAttribute(BackEndNames.LOGIN_STATUS, false);
httpSession.setAttribute(BackEndNames.LOGIN_STATUS, false);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
//adminPage
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession httpSession = request.getSession();
LOG.debug("httpSession ID: " + httpSession.getId());
Enumeration<String> sessionEnum = httpSession.getAttributeNames();
while(sessionEnum.hasMoreElements()){
LOG.debug("SESSION PARAM NAME: " + sessionEnum.nextElement());
}
Account adminUser = (Account) httpSession.getAttribute(BackEndNames.ACCOUNT);
UserDetails userDetails = (UserDetails) httpSession.getAttribute(BackEndNames.USER_DETAILS);
LOG.debug("adminUser: " + adminUser.toString());
LOG.debug("user details for account " + adminUser.getMail() + " are " + userDetails.toString());
request.setAttribute(BackEndNames.USER_DETAILS, userDetails);
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/adminPage.jsp");
requestDispatcher.forward(request, response);
}
JSP Pages
//index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%--java class imports--%>
<%@page import="constantNames.JspFieldsName"%>
<html>
<head>
<title>Login Action</title>
</head>
<body>
<c:set var="account" value="${requestScope.account}" />
<c:set var="userDetails" value="${requestScope.userDetails}"/>
<c:set var="loginStatuts" value="${requestScope.loginStatus}"/>
<c:set var="registrationStatus" value="${requestScope.registrationStatus}"/>
<c:choose>
<c:when test="${loginStatuts}">
<form name="logout" method="post" action="${pageContext.request.contextPath}/logout">
<p>
Welcome ${fn:toUpperCase(userDetails.name)} <br/>
<input type="hidden" value="out" name="logOut">
<button type="submit">LOG OUT</button>
</p>
</form>
<form name="adminPage" method="post" action="${pageContext.request.contextPath}/adminPage">
<button type="submit">TO ADMIN PAGE</button>
</form>
</c:when>
<c:otherwise>
<p><c:out value="${JspFieldsName.LOGIN_TEXT_FIELD}"/></p>
<form name="login" method="post" action="${pageContext.request.contextPath}/login">
<input type="text" name="${JspFieldsName.LOGIN_TEXT_FIELD}"/>
<input type="password" name="${JspFieldsName.LOGIN_PASSWORD_FIELD}"/>
<input type="checkbox" name="saveLogin" value="Save login"/>
<button type="submit">LOG IN</button>
</form>
<form name="register" method="post" action="registerAction.jsp">
<input type="submit" value="Make new Accout"/>
</form>
<c:if test="${registrationStatus}">
<p>Successful</p>
</c:if>
</c:otherwise>
</c:choose>
</body>
</html>
//adminPage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>Title</title>
</head>
<body style="background-color: black">
<c:set var="account" value="${requestScope.account}" />
<c:set var="userDetails" value="${requestScope.userDetails}"/>
<p style="align-content: center; color: orangered">
WELCOME HOME ${userDetails.name} !!
</p>
<form name="back" method="post" action="${pageContext.request.contextPath}/login">
<input type="submit" value="Back">
</form>
</body>
</html>
Here is a part of the log from InteliJ Tomcat log :
Fresh login
[DEBUG] 2019-10-20 20:37:49.088 [http-nio-8080-exec-3] LogIn - httpSession ID: 6FE16EE7F3D96154753F50A9A5AC3551
[DEBUG] 2019-10-20 20:37:49.096 [http-nio-8080-exec-3] LogIn - is httpSession new: false
[DEBUG] 2019-10-20 20:37:49.097 [http-nio-8080-exec-3] LogIn - ACCOUNT FROM SESSION: null
From index jsp to adminPage servlet
[DEBUG] 2019-10-20 20:37:58.074 [http-nio-8080-exec-4] AdminPage - httpSession ID: 6FE16EE7F3D96154753F50A9A5AC3551
[DEBUG] 2019-10-20 20:37:58.074 [http-nio-8080-exec-4] AdminPage - SESSION PARAM NAME: loginStatus
[DEBUG] 2019-10-20 20:37:58.074 [http-nio-8080-exec-4] AdminPage - SESSION PARAM NAME: userDetails
[DEBUG] 2019-10-20 20:37:58.074 [http-nio-8080-exec-4] AdminPage - SESSION PARAM NAME: account
From adminPage jsp to login servlet
[DEBUG] 2019-10-20 20:37:58.806 [http-nio-8080-exec-5] LogIn - httpSession ID: 6FE16EE7F3D96154753F50A9A5AC3551
[DEBUG] 2019-10-20 20:37:58.806 [http-nio-8080-exec-5] LogIn - is httpSession new: false
[DEBUG] 2019-10-20 20:37:58.806 [http-nio-8080-exec-5] LogIn - SESSION PARAM NAME: loginStatus
[DEBUG] 2019-10-20 20:37:58.806 [http-nio-8080-exec-5] LogIn - SESSION PARAM NAME: userDetails
[DEBUG] 2019-10-20 20:37:58.806 [http-nio-8080-exec-5] LogIn - ACCOUNT FROM SESSION: null
What could be the problem and how to fix it ? I ran out of ideeas.