Forums Register Login

sendRedirect Problem

+Pie Number of slices to send: Send
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");
+Pie Number of slices to send: Send
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);
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1279 times.
Similar Threads
How to debug a servlet
sendRedirect
Servlet delivery in Tomcat
HTTP Status 404 servlet error ("The requested resource is not available")
Starting out on Servlets - from Head First Servlets and JSP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:24:25.