Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

java - Servlet mapping: url-pattern for URLs with trailing slash

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 /.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...