28

I have a problem related to the servlet mapping. I have the following in web.xml:

<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

If I access to http://localhost:<port>/MyApp/HelloWorld the servlet HelloWorldServlet is called.

I also want my servelet to respond to http://localhost:<port>/MyApp/HelloWorld/. How can I achieve this effect? I'm developing with NetBeans but it does not allow me to put a pattern ended with /.

4
  • Doesn't it already work? Commented Dec 7, 2010 at 14:19
  • 1
    What happens if you put the trailing "/"? Commented Dec 7, 2010 at 14:20
  • If I edit the xml without the netbeans front-end and deploy the project, images, css, and js are not loaded O.o. Commented Dec 7, 2010 at 16:12
  • If I edit the xml without the netbeans front-end and deploy the project, css, js, images, etc are not loaded O.o. The solution is, as all people have said, is using the syntax /*. With this you can call .../HelloWorld and .../HelloWorld/. If I write .../HelloWorld/foo the same servlet is called (like in this page). If you don't want this effect, you have to use the method HttpServletRequest.getPathInfo() as The Elite Gentleman has said. Then redirect to a jsp to show the error (not found) if the result is diferent to "/". Commented Dec 7, 2010 at 16:19

2 Answers 2

31

After you've added your wildcard on your <url-pattern>

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

You can get the extra path associated with the URL by using HttpServletRequest.getPathInfo().

E.g.

http://localhost:<port>/MyApp/HelloWorld/one/

The result will be

/one/

From the JavaDoc:

Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.

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

Comments

8

Use a wildcard. You can redirect all the traffic going to a specific URL to the same servlet. For example, you can add the following:

<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorld/*</url-pattern>
</servlet-mapping>

This will redirect the URL with a slash to your original servlet.

One thought - this would redirect anything to this URL pattern to the servlet. If you want to have other URL's past this URL, you should create a servlet that will redirect to the correct URL (by looking at the URL specified). Alternatively, you could use a framework that provides mapping for you.

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.