Sumithra
Sumithra

Reputation: 6707

Servlet Mapping URL pattern

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?

Upvotes: 1

Views: 2671

Answers (1)

Bozho
Bozho

Reputation: 597026

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;

Upvotes: 3

Related Questions