This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendRedirect()" Watch "sendRedirect()" New topic


Shravan Durvasula

Joined: Mar 06, 2004
Posts: 19
Hi all:

The name of my web application is webapp1.
This is what i have in my doPost method

I have two questions regarding this method.

1. The string parameter for the sendRedirect() method starts with a "/". Why should i also include the name of the web application as a part of the parameter (in this case webapp1) ? Does'nt the "/" mean, "relative to the current web application" ? If i do need to include the name of the web application, then the HFS book says the other way round on page 136.

2. I tried un-commenting the lines
out.flush() and out.close() which probably means the response has been commited at that stage. As stated in page 137 of HFS I was expecting an IllegalStateException because i am redirecting after commiting the response. But no exception was thrown. What could be the possible reason?

Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59

answers to Your questions:
The HFS is not correct regarding the sendRedirect method.
Qoutation from the API (HttpServletResponse's sendRedirect method):

If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

So it is not relativ to the current web application root.

The same rule is applied for the encodeURL() and encodeRedirectURL() methods.

If You invoke the out.flush() method, the response is commited to the client. It means, that the IllegalStateException() is not visible on the client side, as the response has been already sent.
If You check the server's log file in the ${CATALINA_HOME}/log directory, You can notice, that an IllegalStateException() has been thrown, which is, well, invisible for the client's browser.

Shravan Durvasula

Joined: Mar 06, 2004
Posts: 19
Thanks Gabriel.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

I'm referring to the meaning of Servlet Container root

Say if i've Tomcat, does it mean:
1. Tomcat/ or
2. Tomcat/webapps/ROOT ???

Kindly clarify.........

Cai DongShan
Ranch Hand

Joined: Dec 07, 2004
Posts: 102
If you use default directory when installing Tomcat 5.5, the Servlet Container root directory is as follow.
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps

Best Regards,<br />DongShan<br /> <br />SCJP1.4, SCWCD1.4
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
I hv a confusion here, infact i disagree to the previous post here, let me explain why...........

Every web-app has a doc root, similarly in Tomcat under webapps directory we have ROOT, which happens to be the default app for this server.

if i say:
only, than i get a default app for the server which the ROOT, i think the server doc root is under ROOT (Correct me if I'm wrong....)

So as per my understanding, Servlet Container root shud be under ROOT directory. I may be wrong in this, kindly correct me, if there is any misconception here.

Cai DongShan
Ranch Hand

Joined: Dec 07, 2004
Posts: 102

Just try the following simple program.

1. Create a servlet.

2. Ceate the following directories
%Tomcat Home%/webapps/test
%Tomcat Home%/webapps/ROOT/test

3. Create a test.html and put it under one of above directory.

Try to run the servlet, you should find that '/' in sendRedirect() is
%Tomcat Home%/webapps
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi DongShang,

Well i understood what you are trying to convey here, and i knew this when i was writing my previous post.......
may be i'm unable to convey what i want to.

Let me try once more.....

I think there is a diff in these terms:
1. Doc root of a web app
2. Doc root of the server
3. Servlet Container root
4. Servers doc base

Mind you, that its Servlet Container Root and NOT
Servlet Container DOC root
IMHO, doc base is a place in context (in literal terms..) of a web app, like test is a web app in your case.
So in the webapps folder if we have a folder called test, the doc base for that app would be directly under test directory excluding WEB-INF, as
WEB-INF shud not be served to the clients.
So when i say doc base for the server, i think, i shud be
%Tomcat-Home%/webapps/ROOT/ and not %Tomcat-Home%/webapps/, as webapps itself is not an application.
Hope i'm able to convey this time.
Correct me if i was wrong anywhere.

[ June 07, 2006: Message edited by: Amit Das ]
I agree. Here's the link:
subject: sendRedirect()
Similar Threads
sendRedirect - to what Path? example code enclosed!
Question about redirecting.
IllegalStateException does not happen. Why not?
multipart download servlet