views:

231

answers:

1

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.

A: 

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
This does make me wonder. The `<c:redirect>` was provided for a reason, and yet it seems extremely limited to me.
skaffman
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
I'm using JSF, and not JSP.Is there a tutorial on how to use filters with JSF..?
TheLameProgrammer
JSF runs on top of JSP/Servlet, you can perfectly use Filters. Say, you are even using non-JSF tags in your JSP.
BalusC