response.sendRedirect() and other disasters

J

Jeano

Here is my problem. Let's say I am designing some kind of online
store. If the user attempts to review their account per the
'myAccount.jsp' page and they have not logged in, they should be
redirected to the 'login.jsp' page instead. There are a few additional
considerations:

1. myAccount.jsp doesn't do any actual work. It immediately uses
<jsp:include to call myAccountInner.jsp where all the actual magic
happens. For my particular app, I cannot use a "<%@ include", I must
use a <jsp:include. There is code in myAccount.jsp below the the
invocation of myAccountInner.jsp that I DO NOT want to get executed in
the event of a pending redirect.

2. When the user is done redirecting, I want their location bar to
read http://www.whatever.com/login.jsp.

I've tried seemingly thousands of combinations. I've tried
response.sendRedirect(), <jsp:forward, forwarding to a servlet that
performs a redirect - nothing I do seems to work.

What I am trying to do in my app seems simple enough. Seems like
millions of people would have come across this problem. Anyone have a
solution for me? Examples online? I am going nuts and would
appreciate thoughts that anyone might have. Thanks.

-j


-- myAccount.jsp ------------------------------------------
<%@ page language="java" buffer="16kb" %>

<jsp:include page="myAccountInner.jsp" flush="false" />

<%
// code here that I do not want to get executed
// in the event of a pending redirect
%>
-----------------------------------------------------------


-- myAccountInner.jsp -------------------------------------
<%

// if the user is not logged in, then redirect them to the
// login page

if ( !userLoggedIn )
{
response.sendRedirect("/login.jsp");
}

%>
-----------------------------------------------------------

-- login.jsp ----------------------------------------------
Please log in
<FORM ACTION="whatever..."
..
..
..
-----------------------------------------------------------
 
R

Ryan Stewart

Jeano said:
Here is my problem. Let's say I am designing some kind of online
store. If the user attempts to review their account per the
'myAccount.jsp' page and they have not logged in, they should be
redirected to the 'login.jsp' page instead. There are a few additional
considerations:

1. myAccount.jsp doesn't do any actual work. It immediately uses
<jsp:include to call myAccountInner.jsp where all the actual magic
happens. For my particular app, I cannot use a "<%@ include", I must
use a <jsp:include. There is code in myAccount.jsp below the the
invocation of myAccountInner.jsp that I DO NOT want to get executed in
the event of a pending redirect.

2. When the user is done redirecting, I want their location bar to
read http://www.whatever.com/login.jsp.

I've tried seemingly thousands of combinations. I've tried
response.sendRedirect(), <jsp:forward, forwarding to a servlet that
performs a redirect - nothing I do seems to work.

What I am trying to do in my app seems simple enough. Seems like
millions of people would have come across this problem. Anyone have a
solution for me? Examples online? I am going nuts and would
appreciate thoughts that anyone might have. Thanks.
[...]
You haven't actually said what your problem is, but I assume it's that
response.sendRedirect isn't working in your included page, which is not a
"problem". It is documented behavior for an include: "The included servlet
cannot change the response status code or set headers; any attempt to make a
change is ignored." And a forward simply sends a request to another resource for
it to produce a response, which has nothing to do with the client, and therefore
the URL in the address bar of a browser will not change. From the description
you've given, using an include directive is the only way to achieve what you
want. You might want to describe exactly why you can't use an include directive.
 
J

Jeano

Ryan,

You are correct in that the sendRedirect doesn't work in my included
page. Depending on which of my numerous variations of this code I use,
I get slightly different results - none of which are the desired. I
know that this is a documented behavior, but I have working under the
assumption that what I am actually trying to do has been done somehow
by somebody somewhere.

I am using the dynamic <jsp:include directive instead of the static <%@
include directive because I based on different parameters passed in the
query string, 'myAccountInner.jsp' will be replaced with a dynamic
value. In order to specify the included page at run-time, I have to
use <jsp:include instead of <%@ include. I am surprised this isn't a
common gripe, unless, of course, I am doing something the hard. Hence,
the post. I am looking for a workaround. Thanks.

-j
 
W

Wendy S

Jeano said:
Here is my problem. Let's say I am designing some kind of online
store. If the user attempts to review their account per the
'myAccount.jsp' page and they have not logged in, they should be
redirected to the 'login.jsp' page instead.

I would put a Filter on that URL pattern, and decide whether the user is
logged in or not, then either let them through to the requested page, or
else redirect to login.
 
R

Ryan Stewart

Jeano said:
Ryan,

You are correct in that the sendRedirect doesn't work in my included
page. Depending on which of my numerous variations of this code I use,
I get slightly different results - none of which are the desired. I
know that this is a documented behavior, but I have working under the
assumption that what I am actually trying to do has been done somehow
by somebody somewhere.

I am using the dynamic <jsp:include directive instead of the static <%@
include directive because I based on different parameters passed in the
query string, 'myAccountInner.jsp' will be replaced with a dynamic
value. In order to specify the included page at run-time, I have to
use <jsp:include instead of <%@ include. I am surprised this isn't a
common gripe, unless, of course, I am doing something the hard. Hence,
the post. I am looking for a workaround. Thanks.
Well, in general as you've discovered, there are many variations on how you can
combine things. The specific combination you're trying to make won't work. Your
best bet is to rethink your design. MVC designs and frameworks such as Struts
help to iron out these difficulties but entail a steep learning curve.
 
Joined
Oct 13, 2010
Messages
1
Reaction score
0
I guess it might not be so relevant for you now, but there is another solution I found useful. using window.location to redirect instead of response.sendRedirect() :

- myAccountInner.jsp -------------------------------------
<%

// if the user is not logged in, then redirect them to the
// login page

if ( !userLoggedIn )
{
%>
<script language="JavaScript">
window.location = "login_url.jsp";
</script>
<%
}
%>

Not so elegant but it does the job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top