Quick Servlet question: meaning of getPathInfo()

D

David Lee Lambert

What is the purpose of javax.servlet.http.HttpServlet.getPathInfo()?

I wanted to build a servlet that stores a virtual filesystem in
an SQL database. It just has one class, which overrides doGet and doPut
so far. Here's what WEB-INF/web.xml looks like:

<web-app>
<servlet>
<servlet-name>fs</servlet-name>
<description>Allows GET, PUT, DELETE of virtual files</description>
<servlet-class>com.lmert.slet.FileDB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fs</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

If I request

http://some.host:8080/slet/x/y/z/a.txt

with a web-browser, I need the relative path "x/y/z/a.txt". However,
getPathInfo() always returns an empty string. Is there some other method
I should use instead? Is it possible to do this?
 
L

Lothar Kimmeringer

David said:
What is the purpose of javax.servlet.http.HttpServlet.getPathInfo()?

If the URL to your servlet is e.g.
http://www.example.com/path/to/servlet/MyServlet
the servlet will also be called if you use the URL
http://www.example.com/path/to/servlet/MyServlet/something/else.txt

In that case the Path-Info is
/something/else.txt
with a web-browser, I need the relative path "x/y/z/a.txt". However,
getPathInfo() always returns an empty string. Is there some other method
I should use instead? Is it possible to do this?

Without trying myself: Look into
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
There are a couple of methods and I think getContextPath should
return the part you need.
Another way would be getRequestURL and take the part for yourself.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
A

as4109

Lothar Kimmeringer ha escrito:
If the URL to your servlet is e.g.
http://www.example.com/path/to/servlet/MyServlet
the servlet will also be called if you use the URL
http://www.example.com/path/to/servlet/MyServlet/something/else.txt

In that case the Path-Info is
/something/else.txt

Thanks for the attempted advice. Here is what I did to solve it: I
changed

<url-pattern>/</url-pattern>

to

<url-pattern>/*</url-pattern>

I'm not sure if this behavior is specific to Tomcat, but it appears
that, if the url-pattern only partially matches, the servlet still
gets called, but PATH_INFO will not contain any unmatched path
components up to the query string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top