Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Web-Application: Why is getContextPath empty?

For this URL:
http://localhost:8888/myapp/myservlet/item

In my servlet inside doGet method:

System.out.println("req.getServletPath(): " + req.getServletPath());
System.out.println("req.getRequestURL(): " + req.getRequestURL());
System.out.println("req.getRequestURI(): " + req.getRequestURI());
System.out.println("req.getContextPath(): " + req.getContextPath());
System.out.println("req.getPathInfo(): " + req.getPathInfo());

Console output:

req.getServletPath(): 
req.getRequestURL(): http://localhost:8888/myapp/myservlet/item
req.getRequestURI(): /myapp/myservlet/item
req.getContextPath(): 
req.getPathInfo(): /myapp/myservlet/item

Why is the getContextPath and getServletPath empty?
If it matters this is a (google) web-project in eclipse which gets deployed on jetty.

I am trying to get part of URL that is after the servlet name i.e /item in this case.

like image 214
Jasper Avatar asked Jun 09 '15 16:06

Jasper


People also ask

What is getContextPath in Java?

getContextPath. String getContextPath() Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character.

Where is context path in Web application?

The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath() method. Now that you get the context path, you can pass it to the services that need it.

What is request getContextPath in JSP?

request. getContextPath()- returns root path of your application, while ../ - returns parent directory of a file. You use request. getContextPath(), as it will always points to root of your application. If you were to move your jsp file from one directory to another, nothing needs to be changed.


2 Answers

I find this graphic from HttpServletRequest Path Decoding useful:

enter image description here

It looks like the problem is that the application is deployed on the root, and then the servlet has a path that includes the application name. Attach the web.xml file in order to verify this.

like image 116
AgilePro Avatar answered Oct 10 '22 18:10

AgilePro


About context path - this path is "subpath" for applications loaded to container. As your contextPath is empty then your application configured in container as default application with no contextPath. It's totally right - in most cases you don't really need any context path. But you should be ready to work with it.

About getServletPath - I'm not sure... But it looks like your app manage your servlet, while getServletPath method asks container (and container don't know anything about your servlet). To control what is managed by container and what by application you can see your web.xml file and read all <servlet> and <servlet-mapping> elements

like image 29
Pavel Uvarov Avatar answered Oct 10 '22 16:10

Pavel Uvarov