| Author |
Why encodeURL not including Context name
|
jami siva
Ranch Hand
Joined: Oct 16, 2009
Posts: 46
|
|
Hi,
In my Servlet, I used below code,
out.println("<html><body");
out.println("<a href=\""+response.encodeURL("/PriceRangeServlet")+ "\"> Click me </a>");
out.println("</body></html>");
In response, when I click on Click me link, I am getting HTTP Status 404 - /PriceRangeServlet because of it is ignoring context path of my application. And in URL, it's shown as http://localhost:8080/PriceRangeServlet
instead of http://localhost:8080/testweb/PriceRangeServlet. Here, testweb is my application name(context name).
Why it is Ignoring context name. If I used RequestDispatcher in Servlet, it is working fine.
What is the difference between these two. And how I fix the above Issue.
Thanks,
Siva
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9090
|
|
|
The documentation of encodeURL doesn't say that it will add the context path to the passed path. The encodeURL is used for session tracking when Cookies are disabled. It adds the JSESSIONID to the end of the request path. You'll have to use request.getContextPath to add the context path to your link...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Michal Bogdal
Greenhorn
Joined: Mar 04, 2010
Posts: 3
|
|
jami siva wrote:Hi,
In my Servlet, I used below code,
out.println("<html><body");
out.println("<a href=\""+response.encodeURL("/PriceRangeServlet")+ "\"> Click me </a>");
out.println("</body></html>");
In response, when I click on Click me link, I am getting HTTP Status 404 - /PriceRangeServlet because of it is ignoring context path of my application. And in URL, it's shown as http://localhost:8080/PriceRangeServlet
instead of http://localhost:8080/testweb/PriceRangeServlet. Here, testweb is my application name(context name).
Why it is Ignoring context name. If I used RequestDispatcher in Servlet, it is working fine.
What is the difference between these two. And how I fix the above Issue.
Thanks,
Siva
You can also use taglibrary
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:url value="/PriceRangeServlet" />
...
which add context path to the url and add jsessionid if it is needed
Br
Michal
|
 |
 |
|
|
subject: Why encodeURL not including Context name
|
|
|