Geo
Geo

Reputation: 96767

How can I find out a servlet's URL?

Let's say I have this in my web.xml:


<servlet>
    <description></description>
    <display-name>MainServ</display-name>
    <servlet-name>MainServ</servlet-name>
    <servlet-class>MainServ</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MainServ</servlet-name>
    <url-pattern>/MainServ</url-pattern>
</servlet-mapping>

Imagine I'm in that servlet's doGet method. Is there anyway of getting at the /MainServ value?

Upvotes: 3

Views: 3555

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074028

Via HttpServletRequest.getServletPath; from the Javadoc:

This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.

E.g.:

String path = req.getServletPath();

...if you've called the first argument to doGet req.

Upvotes: 4

Related Questions