开发者

Servlet Mapping URL pattern

开发者 https://www.devze.com 2023-01-25 13:26 出处:网络
webapps ----helloworld ----WEB-INF -----classes-HelloWorldServlet.class |-----lib----servlet-api.jar
webapps
|
|----helloworld
     |
     |----WEB-INF
          |
          |-----classes-HelloWorldServlet.class
          |-----lib----servlet-api.jar
          |-----web.xml

The above is my directory structure开发者_开发知识库. Now in web.xml i don't know what to give in url-pattern for servlet mapping. What should i give there? Which is the url pattern?


The mechanism for mapping servlets is not relevant to the directory structure, as skaffman noted.

Basically, you have two things in web.xml (regarding servlets):

  • the <servlet> tag, which defines the alias for the servlet, and its fully-qualified name (for example com.foo.pkg.YourServlet)

  • the <servlet-mapping> which specifies a url-pattern for a given alias (taken from the <servlet> definitions).

As the name suggest, the url-pattern denotes what URL portion should make the servlet be called. So if you map a given servlet to the url-pattern /myfirstserlet, it will be accessible when the user opens http://localhost:8080/helloword/myfirstservlet, where the first part is the host name and the port, followed by the context name (the name of your webapp), and then the url-pattern

Note: you are currently using the default package (i.e. no package) for your servlet. This is discouraged, so give it some package name. (and put it in WEB-INF/classes/com/foo/pkg/). This is done via specifying package com.foo.pkg;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号