Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why encodeURL not including Context name

 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Michal Bogdal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic