2

Inside a filter I'm trying to send expired sessions back to a login page with a message that the user must login. I prefer to attach the message to the request object, rather than to the session object, so that I don't have to worry about erasing the message later.

When I use inside the filter:

catch (NullPointerException exc)

{

    String message = "You must log in to access this site.";
    url += "/login.jsp";
    request.setAttribute("message", message);
    httpResponse.sendRedirect(url); 
}

The login page does NOT display the message (message is null). However, when I use:

session.setAttribute("message", message);

The message is displayed nicely on the login page.

In servlets, I have no problem attaching and displaying messages using the request object. It's only with filters that I'm noticing this difficulty.

Why is this happening, and can it be prevented? If I must use session when redirecting from a filter, what's the best way to erase the message on the JSP side?

1 Answer 1

3

Use RequestDispatcher instead.

I always refer anyone asking RequestDispatcher vs SendRedirect to this article on The Server Side.

See also ServletRequest#getRequestDispatcher(java.lang.String)

so you do

 request.getRequestDispatcher("/login.jsp").forward(request, httpResponse);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.