Redirect droplet inside include wont redirect

1,166 views
Skip to first unread message

David Herman

unread,
Jul 16, 2008, 2:59:16 PM7/16/08
to ATG_Tech
I have the following setup
test.jsp
<%@ taglib uri="/dspelTaglib" prefix="dspel"%>
<%@ taglib uri="/c" prefix="c"%>
<dspel:page>
<dspel:include page="testtest.jsp" flush="false" />
</dspel:page>>

<%@ taglib uri="/dspelTaglib" prefix="dspel"%>
<%@ taglib uri="/c" prefix="c"%>

testtest.jsp
<dspel:page>asdfasdf
<dspel:importbean bean="/atg/dynamo/droplet/Redirect" />
<dspel:droplet name="Redirect">
<dspel:param name="url" value="http://www.google.com" />
</dspel:droplet>
</dspel:page>

No matter what I try this redirect will not happen, I get no
exceptions or anything

Belmont, Adam

unread,
Jul 16, 2008, 3:21:34 PM7/16/08
to atg_...@googlegroups.com
If your response is already committed (meaning something has already been
written back to the client), then you can't do a redirect. The
response.sendRedirect() call will throw an IllegalStateException.
That exception should get rendered in the log file if you are getting it.

Also, I believe that once you do an include in j2ee, you cannot change
headers. Since a "sendRedirect()" method requires a header to be changed
that means you can't do a redirect inside of an include.

(See j2ee 1.4 spec section 8.3)
"It cannot set headers or call any methods that affects the headers of the
response. Any attempt to do so must be ignored."

As a workaround you could use the static include directive for your target
page:
<%@ include file="relativeURL" %>

That has different behavior since it combines the two pages into one, then
compiles them into a single class. Sometimes that class can become larger
than java will allow and the page won't compile. So be careful about too
many static include directives.

-- Adam

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ATG_Tech" group.
To post to this group, send email to atg_...@googlegroups.com
To unsubscribe from this group, send email to
atg_tech-u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/atg_tech?hl=en
-~----------~----~----~----~------~----~------~--~---

David Herman

unread,
Jul 16, 2008, 3:30:15 PM7/16/08
to ATG_Tech
Yes I know I can do the static include I was just hoping to put the
redirect into another file so that it could be included in multiple
areas of the site and be maintained in one location.
>  smime.p7s
> 4KDownload

Gordon Cooke

unread,
Jul 16, 2008, 3:39:04 PM7/16/08
to ATG_Tech
In the page directive you can increase the buffer size. I would have
to go back and check but I am pretty sure I have done this inside
includes. I have found that the default page buffer size in JBoss is
much smaller then the one that was in DAS. By increasing the buffer
you may be able to delay/hold off the response being commited.

you can increase the buffer size in the page directive like:

<%@ page ...... buffer="none|8kb|sizekb" ... %>

Be careful making the buffer too large as it can burn memory quickly
if your pages are large.

Gordon

Jeremy Sears

unread,
Jul 16, 2008, 4:31:05 PM7/16/08
to atg_...@googlegroups.com
You CAN use a static include to include a page from multiple locations, but maintain only one included file.  It's roughly equivalent to doing a <dsp:include>, but the code from the statically included JSP is compiled into the parent JSP, rather than referencing it. From a developers perspective, there is little difference for your example. 

One thing to note is that, in your example, you would need to ensure that no white space had been emitted to the page.  This may mean that you'd need to write the taglib directives and static include in a single line with no whitespace between tags.
--
Jeremy Sears

Belmont, Adam

unread,
Jul 16, 2008, 5:09:15 PM7/16/08
to atg_...@googlegroups.com

Even if there is no whitespace and/or the page buffer is set large enough that it doesn’t flush before the include, I think it just won’t work once a call to RequestDispatcher.include() has been made.

The target “servlet” (in this case a jsp page) can’t set headers. So that means it can’t redirect.

 

The include method of the RequestDispatcher interface may be called at ANY time. The target servlet of the include method has access to all aspects of the request object, but its use of the response object is more limited. It can only write information to the ServletOutputStream or Writer of the response object and commit a response by writing content past the end of the response buffer, or by explicitly calling the flushBuffer method of the ServletResponse interface. It CANNOT set headers or call any method that affects the headers of the response. Any attempt to do so must be ignored.

The forward method of the RequestDispatcher interface may be called by the calling servlet ONLY when NO output has been committed to the client. If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet's service method is called. If the response has been committed, an IllegalStateException must be thrown.”

I don’t see that restriction listed in the paragraph for RequestDispatcher.forward(). So dspel:forward might be another workaround instead of the static include directive. The forward still has the restriction that the response can’t be committed. So you’ll need to make sure there is no whitespace being written before the Redirect droplet.

Performance wise it’s probably better to do the static include directive and avoid the overhead of doing a forward just to trigger the redirect.

 -- Adam


Subodh Sharma

unread,
Jul 17, 2008, 7:52:50 AM7/17/08
to atg_...@googlegroups.com
Try putting the redirect droplet just after <dspel:page> tag.


Wilco Boumans

unread,
Jul 17, 2008, 4:33:18 PM7/17/08
to atg_...@googlegroups.com

That's right,

because the part before the include may include a (partial) page, the
redirect fails.
You cannot reply with a page and then halfway decide that you meant to
return a redirect...you need to decide very early whether a redirect
is required instead of a page rendering.


Wilco

Devon Hillard

unread,
Jul 17, 2008, 5:24:24 PM7/17/08
to ATG_Tech
Ignoring the dynamic versus static include issue, you CAN write out to
the response and then later decide to do a redirect, IF (as Gordon
mentioned above) your page buffer is large enough.

In this case, using a static include, and upping the page buffer if
needed, should meet all of David's needs.
Reply all
Reply to author
Forward
0 new messages