1

I am trying display a session attribute on this jsp page:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="true" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>title</title>
        <link rel="stylesheet" href="/css/style.css">
    </head>
    <body>
    <c:choose>
        <c:when test="${error}">
            Erro.
            <br />
        </c:when>
        <c:otherwise>
            <c:out value="${sessionScope.login}"/> | <a href="<c:url value = "/logout"/>">Sair</a>.
            <br />
        </c:otherwise>
    </c:choose>
    <script src="/js/script.js"></script>
    </body>
</html>

this page is forwarded from this servlet:

package org.loja.app.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String login = request.getParameter("login");
        String password = request.getParameter("password");

        if(password.equals("123")) {
          HttpSession session = request.getSession();
          session.setAttribute("login", login);
          request.setAttribute("error", false);
          request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
        } else {
          request.setAttribute("error", true);
          request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
        }
    }
}

when I open the page on the browser, instead of the attribute value, I get the text ${sessionScope.login}.

What I am missing here?

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188

1 Answers1

0

There doesn't seem to be anything that is wrong by the looks of it. However here are a couple things i would try:

Remove this tag from the top:

<%@ page session="true" %>

The default behaviour is set to true anyway. There's no need to declare it again. Maybe this is invalidating the session variables you set.

Server configuration

Some servers have configuration settings where you need to enable session support. Perhaps this is not turned on or has been turned off.

You say that <c:out doesn't work but <c:url does. If <c:url works but <c:out doesn't then maybe your .jar jstl file is corrupted? But i highly doubt that. I think the problem lies with just that the sessionScope isn't set. Why not just try and access the variable directly? sessionScope isn't necessary.

Try:

   <c:out value="${login}"/> | <a href="<c:url value = "/logout"/>">Sair</a>.

Also try it without JSTL:

 ${login} | <a href="/logout">Sair</a>.

Try setting it in the jsp:

<c:set var="testVar" value="Hello world" scope="session"/>
<c:out value="${testVar}"/>

Lastly, make sure you are actually setting the variable. Test for it like this:

    HttpSession session = request.getSession(); //move outside to test it 
          session.setAttribute("login", login);  //move outside to test it 

if(password.equals("123")) {

          request.setAttribute("error", false);
          request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
        } else {
          request.setAttribute("error", true);
          request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
        }

Try these troubleshooting steps and hopefully it should help you narrow down the problem.

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • Since yesterday, I have tried all the troubleshooting steps you mentioned, but none solve the problem. I tested other JSTL tags, like c:forEach, c:forTokens, and these work fine too, like c:url, but tags like c:out and c:if do not work (seems like what not working it's the processing of `${...}` variables). – Kleber Mota Oct 30 '18 at 19:29
  • so you tried it without jstl and it didn't work? Do you want to teamviewer really quickly so i can see what is happening exactly? – Jonathan Laliberte Oct 30 '18 at 19:32
  • 1
    never mind, i already solve the problem (with the orientation from the answer in this question: https://stackoverflow.com/questions/30080810/el-expressions-not-evaluated-in-jsp). – Kleber Mota Oct 30 '18 at 19:51