The client accesses the resources in the Web server through the URL address. If the Servlet program wants to be accessed by the outside world, it must be mapped to a URL address. Often, the URL address is inconsistent with the physical path (storage location on the hard disk) of the Servlet program, so it is called a virtual path. The corresponding relationship between Servlet and virtual path is called Servlet virtual path mapping.
Servlet virtual path mapping can be divided into two categories:
- Single mapping
- Multiple mapping
The following describes how to implement single mapping and multiple mapping.
Servlet single mapping
Servlet single mapping means that a servlet is mapped to only one virtual path.
There are two ways to implement Servlet single mapping:
- Using web XML to achieve a single mapping;
- Use @ WebServlet to implement a single mapping.
1. web.xml implements a single mapping
On the web In the XML file, the < Servlet > and < Servlet mapping > elements are used to realize a single Servlet mapping. The code is as follows.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>net.biancheng.www.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> </web-app>
The above labels are described as follows:
- The < Servlet > element is used to register the Servlet, that is, to give the Servlet a unique name.
- < Servlet > contains two main sub elements < Servlet name > and < Servlet class >, which are respectively used to specify the name of the Servlet and the fully qualified name of the Servlet (package name + class name).
- The < Servlet mapping > element is used to define the mapping between the Servlet and the virtual path.
- < Servlet mapping > contains two sub elements < Servlet name > and < URL pattern >, which are used to specify the name and virtual path of the Servlet respectively.
2. @WebServlet implements a single mapping
In the @ WebServlet annotation, the value attribute is generally used to realize a single Servlet mapping. The code is as follows.
package net.biancheng.www; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
You can also use the urlPatterns attribute to implement a single Servlet mapping. The code is as follows.
package net.biancheng.www; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/myServlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
Servlet multiple mapping
Multiple mapping of servlets means that a Servlet can be mapped to multiple virtual paths. At this time, the client can access the same Servlet through multiple paths.
There are three ways to implement Servlet multi mapping:
- Configure multiple < servlet mapping > elements.
- Configure multiple < URL pattern > child elements.
- Use a string array in the urlPatterns property of the @ WebServlet
1. Configure multiple < servlet mapping > elements
Before the Servlet 2.5 specification, < servlet mapping > elements were allowed to contain only one < URL pattern > sub element. To realize multiple mapping of Servet, you can only configure multiple < servlet mapping > elements.
Take serveltDemo as an example, on the web Two < servlet mapping > elements are configured in XML, and the code is as follows.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>net.biancheng.www.MyServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet2</url-pattern> </servlet-mapping> </web-app>
2. Configure multiple < URL pattern > sub elements
Starting from Servlet 2.5, < Servlet mapping > elements can contain multiple < URL pattern > sub elements, and each < URL pattern > represents the mapping rule of a virtual path. Therefore, multiple mapping of servlets can also be realized by configuring multiple < URL pattern > sub elements in a < Servlet mapping > element.
Take servletDemo as an example, add two < URL pattern > sub elements under the < servlet mapping > element. The code is as follows.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>net.biancheng.www.MyServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet</url-pattern> <url-pattern>/myServlet3</url-pattern> </servlet-mapping> </web-app>
3. @WebServlet realizes multiple mapping
Servlet 3.0 adds support for @ WebServlet annotation. We can specify a set of mapping rules in the form of string array in urlPatterns attribute to realize multiple mapping of servlets.
Take servletDemo as an example, add a group of virtual paths in the urlPatterns attribute of the @ WebServlet annotation. The code is as follows.
@WebServlet( urlPatterns = { "/myServlet", "/myServlet4" })