0

Redirecting to login.php action if role!='user'. Redirection works good, but the content of msg attribute is not displayed. Code for index page:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <c:if test="${sessionScope.role ne 'user'}">
    <%
        request.setAttribute("msg", "Please Login to select Theme");
        response.sendRedirect("login.php");
    %>
 </c:if>

Code for login page:

<h1>${requestScope.msg}</h1>

Kindly help on this.

CᴴᴀZ
  • 521
  • 7
  • 20
  • When you were typing your question, there was this nice big orange **How to Format** box to the right of it telling you that you can format code by indenting it four spaces, thereby making the code easily read. *Edit: Govan has been kind enough to do it for you this time, but as this is your seventh question, really you should be doing this yourself now.* – T.J. Crowder Nov 18 '13 at 08:39
  • @T.J.Crowder:Sorry Sir, was in hurry for the lunch as they would have deprived me if I had delayed further. Will abide with the format rules. :) – CᴴᴀZ Nov 18 '13 at 09:14

3 Answers3

0

You can't pass request attribute value with sendRedirect. Use session instead of request.

 <%
    session.setAttribute("msg", "Please Login to select Theme");
    response.sendRedirect("login.php");
 %>

At login.php page:

<h1>${sessionScope.msg}</h1>

or

<h1>${msg}</h1>
Masudul
  • 21,823
  • 5
  • 43
  • 58
0
  • Use a request dispatcher's forward method if you want to pass data using request scope.
  • If getting the message to the next page is the only thing expected (i.e. request scope not being mandatory), then use session scope to save the msg and retrieve it on the next page (which we reached using sendRedirect). do remember to delete the key from session after usage to avoid bloating of session scope.
Prashant P
  • 23
  • 5
0

This solved my purpose:
request.getRequestDispatcher("login.php").forward(request, response);

Source of help. Thanks for mentioning that response.sendRedirect(); needs Session Scope.

Community
  • 1
  • 1
CᴴᴀZ
  • 521
  • 7
  • 20