8

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.

5
  • You think it should return /myapp? Why? Commented Jun 9, 2015 at 16:54
  • Sotirios> It should not be blank - isn't it? Commented Jun 9, 2015 at 17:52
  • It depends on how you configure your container. Commented Jun 9, 2015 at 17:56
  • Which container are you using? Chack if your application is deployed as a root. If so there would me no context. Commented Jun 10, 2015 at 6:06
  • This gets deployed on Jetty from within Eclipse, as you can see in the URL: localhost:8888/myapp/myservlet/item , the 'myapp' part is the application name, so its not root i guess. I am trying to get the last part of URL beyond 'myservlet/' i.e item' - how do i get that? Commented Jun 10, 2015 at 10:29

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.