0

I want the user to be redirected to Login.jsp if the "role" stored in Session Scope doesn't match with the Request Parameter "accessRole"

HomePage.jsp

<jsp:include page="Header.jsp">
<jsp:param value="d" name="accessRole" />
</jsp:include>

Header.jsp

<c:if test="${sessionScope.role!=param.accessRole}">
<c:redirect url="Login.jsp"/> 
</c:if>

The above code does not perform the redirection as expected.

I tried using ExternalContext's redirect() and jsp:forward in place of <c:redirect>, but nothing works.

2 Answers2

0

You cannot redirect inside a JSP include, it's often already too late. If you have read the appserver logs, you should have seen an IllegalStateException: response already committed (just because the content of the parent page is already been sent to the response).

The real solution for this is to implement a Filter which is mapped on the url-pattern covering the parent page.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This does make me wonder. The `` was provided for a reason, and yet it seems extremely limited to me. – skaffman Dec 21 '09 at 12:38
  • You may have luck with it inside the first 2KB of the response (i.e. at top of parent JSP page). But indeed, it is somewhat superfluous, I have also never used it. – BalusC Dec 21 '09 at 12:49
  • I'm using JSF, and not JSP. Is there a tutorial on how to use filters with JSF..? – TheLameProgrammer Dec 21 '09 at 16:11
  • JSF runs on top of JSP/Servlet, you can perfectly use Filters. Say, you are even using non-JSF tags in your JSP. – BalusC Dec 21 '09 at 17:47
0

JSP includes are not allowed to send a redirect. You'll have to use:

<jsp:include.directive file="url"/>
sparkyspider
  • 13,195
  • 10
  • 89
  • 133