Java

How to configure web.xml to map a servlet to more than one url pattern in Java?

Here is a sample web.xml file that maps multiple URLs to the same servlet.


<web-app>
	<display-name>testApplication</display-name>
	
	<servlet>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>com.devesh.TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/</url-pattern>
		<url-pattern>/xxx/*</url-pattern>
	</servlet-mapping>
</web-app>

Advertisements

5 thoughts on “How to configure web.xml to map a servlet to more than one url pattern in Java?

  1. Cool, didn’t know you could map 2 URLs to 1 servlet. That could be useful to me. You might also want to add that you can pass parameters to a servlet by adding the following stanza in the block

    myFile
    /WEB-INF/classes/myFile.txt

    Once in the servlet you can get that param by overriding the servlet’s init() method ๐Ÿ™‚

  2. Cool, didnโ€™t know you could map 2 URLs to 1 servlet. That could be useful to me. You might also want to add that you can pass parameters to a servlet by adding the following stanza in the <servlet> block

    <init-param>
    <param-name>myFile</param-name>
    <param-value>/WEB-INF/classes/myFile.txt</param-value>
    </init-param>

    Once in the servlet you can get that param by overriding the servletโ€™s init() method ๐Ÿ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s