Table of Contents

Sample servlet mapping

In a web application, we may have multiple servlets with each one assigned a particular task.We have servlet and servlet-mapping tags in web.xml. Both these tags are used together to map servlets to a URL.

I am reproducing a basic web.xml which creates mapping for servlets:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<servlet>
		<servlet-name>sample</servlet-name>
		<servlet-class>com.example.HelloServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>sample</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
</web-app>

Here we have a servlet named sample and class as com.example.HelloServlet. By using servlet-mapping tag, we have mapped this servlet to the URL http://localhost:8080/Hello/Hello. We should have Hello word two times in the servlet because we have named the web application as Hello. The name of web application folder should come after http://localhost:8080.

Extension based servlet mapping

Till now it should be very simple. Now let us try to use extension based mapping of servlets. Now we shall have sampleExtension servlet mapped to *.Hello requests. Here * is a wild card character and it means that Hello.Hello, New.Hello and Test.Hello will invoke this servlet. In fact all URL’s ending with .Hello will invoke our sampleExtension servlet. The web.xml for extension based mapping of servlet is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<servlet>
		<servlet-name>sample</servlet-name>
		<servlet-class>com.example.HelloServlet</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>sampleExtension</servlet-name>
		<servlet-class>com.example.HelloServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>sample</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>sampleExtension</servlet-name>
		<url-pattern>*.Hello</url-pattern>
	</servlet-mapping>
</web-app>

I would recommend that you download the application and deploy it on tomcat. The download link is:

Please note that you need to use JDK/JRE 1.6 and Tomcat 7.0 to run this sample web application.

To verify what we have discussed till now, hit the following URL’s after deploying the above sample application in webapps folder of tomcat.

http://localhost:8080/Hello/Hello

http://localhost:8080/Hello/Hello.Hello

http://localhost:8080/Hello/My.Hello

http://localhost:8080/Hello/His.Hello

http://localhost:8080/Hello/Their.Hello

http://localhost:8080/Hello/Unknown.Hello

http://localhost:8080/Hello/*.Hello

http://localhost:8080/Hello/HelloWorld

JSP mappings in web applications

As with servlets, one can also map JSP pages to URL’s. To do so, we need help of web.xml file again. The mapping of JSP files is not same as of servlets. For JSP URL mapping, we have two ways as shown below:

Direct JSP file mapping

If you have a JSP say hello.jsp and want to invoke it with URL as http://localhost:8080/Hello/MyWorld then you need to create a mapping as shown below in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<servlet>
		<servlet-name>sample</servlet-name>
		<servlet-class>com.example.HelloServlet</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>sampleExtension</servlet-name>
		<servlet-class>com.example.SampleHelloServlet</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>myjsp</servlet-name>
		<jsp-file>/hello.jsp</jsp-file>
	</servlet>

	<servlet-mapping>
		<servlet-name>sample</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>sampleExtension</servlet-name>
		<url-pattern>*.Hello</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>myjsp</servlet-name>
		<url-pattern>/MyWorld</url-pattern>
	</servlet-mapping>
</web-app>

As you can see that in the servlet tag for JSP, we have replaced servlet-class tag with jsp-name tag. The above snippet has the mappings for servlets that we discussed in the beginning.

Mapping servlet corresponding to JSP

Another method for mapping a URL to JSP page is by creating an entry for the servlet generated by server after JSP translation. The process for doing so is:

  • Add jsp to the web application.
  • Start server.
  • Go to $TOMCAT_HOMEworkCatalinalocalhost[webapp]orgapachejsp folder and copy the class file corresponding to JSP.
  • Paste this class file inside WEB-INF folder of application. Give attention to package structure of translated servlet and create same package structure in WEB-INF folder.
  • Create mapping in web.xml for the translated servlet and hit the URL configured by you in web.xml.

You may download the following application where I have created a mapping for sample.jsp in the web.xml by using the above steps.

After deploying the application, please hit the following URL to access sample.jsp.

http://localhost://8080/MySample

The above application has all the servlet and jsp mappings discussed in this tutorial.

Map Servlet and JSP to URL admin Java EE
Table of Contents 1. Sample servlet mapping 2. Extension based servlet mapping 3. Direct JSP file mapping 4. Mapping servlet corresponding to JSP Sample servlet mapping In a web application, we may have multiple servlets with each one assigned a particular task.We have servlet and servlet-mapping tags in web.xml. Both these tags are used together...
Table of Contents <div id="toc"> 1. <a href="#servletmapping">Sample servlet mapping</a> 2. <a href="#extensionmapping">Extension based servlet mapping</a> 3. <a href="#directjspmapping">Direct JSP file mapping</a> 4. <a href="#mappingservletjsp">Mapping servlet corresponding to JSP</a> </div> <a name="servletmapping"></a> <h3>Sample servlet mapping</h3> <p> In a web application, we may have multiple servlets with each one assigned a particular task.We have servlet and servlet-mapping tags in web.xml. Both these tags are used together to map servlets to a URL. I am reproducing a basic web.xml which creates mapping for servlets: </p> 1 <p> Here we have a servlet named sample and class as com.example.HelloServlet. By using servlet-mapping tag, we have mapped this servlet to the URL http://localhost:8080/Hello/Hello. We should have Hello word two times in the servlet because we have named the <a href="http://www.javaexperience.com/performance-management-for-web-applications/" title="Performance Management in web applications">web application</a> as Hello. The name of web application folder should come after http://localhost:8080. </p> <a name="extensionmapping"></a> <h3>Extension based servlet mapping</h3> <p> Till now it should be very simple. Now let us try to use extension based mapping of servlets. Now we shall have sampleExtension servlet mapped to *.Hello requests. Here * is a wild card character and it means that Hello.Hello, New.Hello and Test.Hello will invoke this servlet. In fact all URL's ending with .Hello will invoke our sampleExtension servlet. The web.xml for extension based mapping of servlet is: </p> 1 <p> I would recommend that you download the application and <a href="http://www.javaexperience.com/deploy-servlet-on-tomcat/">deploy it on tomcat</a>. The download link is: <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg" alt="Servlet Mapping code"><a href="http://www.javaexperience.com/wp-content/uploads/2012/08/Hello.zip"><br><br><strong>Servlet JSP Mapping</strong></a></div> Please note that you need to use JDK/JRE 1.6 and Tomcat 7.0 to run this sample web application. To verify what we have discussed till now, hit the following URL's after deploying the above sample application in webapps folder of tomcat. http://localhost:8080/Hello/Hello http://localhost:8080/Hello/Hello.Hello http://localhost:8080/Hello/My.Hello http://localhost:8080/Hello/His.Hello http://localhost:8080/Hello/Their.Hello http://localhost:8080/Hello/Unknown.Hello http://localhost:8080/Hello/*.Hello http://localhost:8080/Hello/HelloWorld </p> <h2>JSP mappings in web applications</h2> As with servlets, one can also map JSP pages to URL's. To do so, we need help of web.xml file again. The mapping of JSP files is not same as of servlets. For JSP URL mapping, we have two ways as shown below: <a name="directjspmapping"></a> <h3>Direct JSP file mapping</h3> If you have a JSP say hello.jsp and want to invoke it with URL as http://localhost:8080/Hello/MyWorld then you need to create a mapping as shown below in web.xml: 1 As you can see that in the servlet tag for JSP, we have replaced servlet-class tag with jsp-name tag. The above snippet has the mappings for servlets that we discussed in the beginning. <a name="mappingservletjsp"></a> <h3>Mapping servlet corresponding to JSP</h3> Another method for mapping a URL to JSP page is by creating an entry for the servlet generated by server after JSP translation. The process for doing so is: <ul> <li>Add jsp to the web application.</li> <li>Start server.</li> <li>Go to $TOMCAT_HOMEworkCatalinalocalhost[webapp]orgapachejsp folder and copy the class file corresponding to JSP.</li> <li>Paste this class file inside WEB-INF folder of application. Give attention to package structure of <a href="http://www.javaexperience.com/understanding-jsp-lifecycle/" title="Understanding JSP Lifecycle">translated servlet</a> and create same package structure in WEB-INF folder.</li> <li>Create mapping in web.xml for the translated servlet and hit the URL configured by you in web.xml.</li> </ul> You may download the following application where I have created a mapping for sample.jsp in the web.xml by using the above steps. <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg" alt="Sample servlet application"><a href="http://www.javaexperience.com/wp-content/uploads/2012/08/Hello.zip"><br><br><strong>Servlet JSP Mapping</strong></a></div> After deploying the application, please hit the following URL to access sample.jsp. http://localhost://8080/MySample The above application has all the servlet and jsp mappings discussed in this tutorial.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.