• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why encodeURL not including Context name

 
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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
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
 
Paper beats rock. Scissors beats tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic