Customize Servlet URL
This tutorial is an additional work of the previous lab. It allows a Servlet can be accessed by different URLs.
-
Create a file: web.xml in the WebContent→WEB-INF folder
Insert the following code into the
web.xml file:
<web-app>
<servlet>
<servlet-name>HelloServ</servlet-name>
<servlet-class>first.servlet.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServ</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloServ</servlet-name>
<url-pattern>/hello_world</url-pattern>
</servlet-mapping>
</web-app>
The tag <servlet-name> defines your servlet; <servlet-class> points to the class that implements the servlet and <servlet-mapping> declares the mapping between your servlet with different URLs.
Save the file.
Right click on the servletlabs project, select Run As→Run on Server. Finish.
Select Restart server. Finish.
Now, the “HelloWorld” servlet can be accessed by different URLs:
Exercises
Back to top