Question

I try setting an attribute for a session and want to assign a String to it, that is displayed later in a jsp. I have the following code:

The System.out results in null even thought the String was set to the error message? What am I doing wrong?

    error = "Something";
        session.setAttribute("error", error);
        System.out.println("This is get Attr: " +session.getAttribute(error));

I added the following lines of code now:

    RequestDispatcher disp = req.getRequestDispatcher("error.jsp");
        disp.forward(req, resp);

and in my jsp:

Hello ${error}

and it is displaying ${error} instead of the value!

Was it helpful?

Solution

You need to access the object by key:

System.out.println("This is get Attr: " +session.getAttribute("error"));

In your example, you are using the value (i.e.the object reference error)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top