hqt
hqt

Reputation: 30284

Servlet: get query string but just return empty

I'm using mediator design pattern for my web project. There is a front controller to take care all requests, and base on each request to find suitable servlet. In web.xml, I try this:

<servlet-mapping>
         <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

And then a request like localhost:8080/project_name/a/b/c/d.jsp I want to get the string a/b/c/d.jsp but when I use: request.getServletPath() I get empty string.

Thanks :)

Upvotes: 0

Views: 778

Answers (1)

bedrin
bedrin

Reputation: 4584

You should use getPathInfo() method instead

getServletPath() method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

getPathInfo() method returns path information follows the servlet path but precedes the query string and will start with a "/" character.

Upvotes: 5

Related Questions