• Post Reply Bookmark Topic Watch Topic
  • New Topic

sendRedirect Problem

 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
I have some problem in understanding sendRedirect.

I have written one servlet for redirecting to a different servlet
/web/HelloWorld -target - to which TestRedirect servlet should be redirected
/web/TestRedirect - redirecting servlet
My package in tomcat
is webapps\karthik\web-inf\classes\com\web\HelloWorld
webapps\karthik\web-inf\classes\com\web\TestRedirect

i have written response.sendRedirect("HelloWorld"); in TestRedirect

FYI http:\\localhost:8080\karthik\index.html is the page

I clicked a link to goto TestRedirect which directs me to HelloWorld

HTML FILE HAS THIS
<FORM method = "GET" action = "web/TestRedirect">

After clicking the link servlet was redirected to my target HelloWorld servlet and address bar value changed to karthik\web\HelloWorld
BUT
when i gave \web\HelloWorld in the TestRedirect

Ex: response.sendRedirect("\web\HelloWorld");
it was not working and always it goes to root directory which is contradictory to kathy sierra book.

when i give \ it should go to karthik directory.
from http:\\localhost:8080\karthik\web\TestRedirect
i.e http:\\localhost:8080\karthik\web\HelloWorld


but it goes to http:\\localhost:8080\web\HelloWorld
It is not taking my web app
can anyone explain it please
My web.xml
<servlet>
<servlet-name>Redirect</servlet-name>
<servlet-class>com.web.TestRedirect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Redirect</servlet-name>
<url-pattern>/web/TestRedirect</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.web.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/web/HelloWorld</url-pattern>
</servlet-mapping>


JAVA FILE HAS THIS
response.sendRedirect("/web/HelloWorld");
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The send Redirect does a client side redirect. That means that the web browser generates the request to the URL you provide. The browser will always translate urls starting with "/" from the top of the domain (http://localhost:8080/) not the web application (http://localhost:8080/karthak/) since it doesn't know anything about web applications. (this is different then how includes and forwards work, since those things are done on the server - which knows about servlet contexts...)

Your alternatives are this:
1) Use relative addresses. You are inside http://localhost:8080/karthak/web/TestRedirect and you want to get to http://localhost:8080/karthak/web/HellowWorld. Relatively, the address is just sendRedirect("HellowWorld")

2) Use the absolute URL sendRedirect("http://localhost:8080/karthak/web/HelloWorld");

3) (my choice) pass the url through the encodeRedirectURL method. This will fix the URL relative to the servlet context and has the benefit of maintaining user session if they decide to turn cookies off:
String sendTo = response.encodeRedirectURL("/web/HelloWorld");
response.sendRedirect(sendTo);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!