1
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?

1
  • 3
    The directory structure is irrelevant to the url-pattern. It's whatever you want it to be. Commented Nov 18, 2010 at 13:17

1 Answer 1

3

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;

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

3 Comments

If i want something like localhost:8080/helloword/war/myfirstservlet, how can it be achieved?
map it to /war/myfirstservlet, and place it in webapps/helloworld
thank you, but i don't think i understand, i tried it, please see this question stackoverflow.com/q/5650136/543544

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.