13

I'm trying to do something that looks small but it's failing. I'm trying to send a response message back to a jsp when login fails but not being able. As of now I can only redirect back to the jsp but cannot display a message from the servlet on it. This is the servlet part of the redirection:

                if (count > 0) {
                res.sendRedirect("adminHome.jsp");
            } else {

                res.sendRedirect("index.jsp");
            }

I tried to print a message using PrintWriter and the redirect but failed because I couldn't get how to receive the message in the JSP. I also read that I shouldn't redirect but rather I should just forward from the servlet. How can I do this? Please help with the code patch to forward from servlet as well as that one to receive in JSP. Thanks

1
  • Never ever use response.getWriter() or getOutputStream() in servlet whenever you use forward/redirect. You will risk IllegalStateExceptions.
    – BalusC
    May 18, 2011 at 16:52

3 Answers 3

37

If you insist to use redirect instead of forward, then you have 2 options:

  1. Pass the message as request parameter

    String message = "hello";
    res.sendRedirect("adminHome.jsp?message=" + URLEncoder.encode(message, "UTF-8"));
    

    so that you can display it in JSP as follows

    <p>Message: ${param.message}</p>
    

    It's only visible in the browser address bar as well and you aren't able to pass non-standard Java objects this way.

  2. Store it in session

    String message = "hello";
    req.getSession().setAttribute("message", message);
    res.sendRedirect("adminHome.jsp");
    

    so that you can display (and remove) it in JSP as follows:

    <p>Message: ${message}</p>
    <c:remove var="message" scope="session" /> 
    

    Removing is important, otherwise it sticks there for the entire session.


However, if you're open to using forward instead of redirect, it's more elegant:

String message = "hello";
req.setAttribute("message", message);
req.getRequestDispatcher("/adminHome.jsp").forward(req, res);

and display it as follows in JSP

    <p>Message: ${message}</p>

See also:

4
  • 2
    Thanks @BalusC, I tried all your options and they give the display I need. Only problem is the message sticks to the page even when I restart the application. How can I remove it such that it only displays when username and password are incorrect? Now it displays the message even before username and password are entered
    – ken
    May 18, 2011 at 18:19
  • Which way have you used? If you've set it in the session without using <c:remove>, then it will indeed stick the entire session. Shutdown and restart the webbrowser. As to the right way of displaying validation messages, click the Our Servlets wiki page link at the "See also" section of my answer.
    – BalusC
    May 18, 2011 at 18:22
  • I restarted the browser and the message was gone. Thank you very much
    – ken
    May 18, 2011 at 18:49
  • yes adding and removing both are important,nice answer... Apr 21, 2022 at 16:53
3

Set your message as an attribute of the request object:

request.setAttribute("message", messageString);

Use a RequestDispatcher to dispatch the request and response objects to the jsp:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("urlString");
dispatcher.forward(request, response);

Access it in your JSP using Expression Language syntax:

${message}
1
  • Thanks @elekwent, Only problem now is it displays message on page even when nothing has been entered. How can I ensure that message only comes after something has been entered?
    – ken
    May 18, 2011 at 18:22
2

You can save the message in the request and then forward to the jsp rather than redirect.

    request.setAttribute("msg", "The actual error message to be displayed");

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/yourpage.jsp");
    rd.forward(request, response);

Then on the jsp page you have access to the message via the request.

1
  • Thanks. It works though message seems to stick on page. How can it be removed
    – ken
    May 18, 2011 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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