The configuration of the Tomcat file path Servelet and the path to Web-inf in Maven

Source: Internet
Author: User

The composition structure of the Tomcat javaweb application

When developing javaweb applications, different types of files have strict storage rules, which may not only make the web app inaccessible, but also cause the Web server to start an error

  

Webroot→web the directory where the application is located, in general, the virtual directory to be configured in this folder.

┝web-inf: This folder must be in the Webroot folder and must be named in this form, with uppercase letters.

┝web.xml: configuration file, format required, this file must be named in this form, and must be placed in the Web-inf folder.

The format of Web. XML can be referenced directly from Tomcat: Find webapps\root\ in the Tomcat directory Web-inf the Web. xml file in this directory, copy this file to our new Web-inf folder, modify the Web. xml file, delete the comments inside, leaving only the code shown below:

Xml:

1 <?xml version= "1.0" encoding= "Iso-8859-1"?> 2 <web-app xmlns= "Http://java.sun.com/xml/ns/javaee" 3    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 4    xsi:schemalocation= "http://java.sun.com/xml/ns/ Java EE http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "5    version=" 2.5 "> 6  7   <display-name >welcome to tomcat</display-name> 8   <description> 9      Welcome to Tomcat10   </description >11 </web-app>

This is the format of the Web. xml file

When using MAVEN development Yes, the Src/main/webapp path saves the Web-inf file, Web-inf in the CNOOC Web. XML configuration file, which can be added to the servlet configuration.

Servlet Development Note Details 5.1, servlet access URL mapping configuration

Because the client accesses the resources in the Web server through a URL address, the servlet program must map the servlet program to a URL address if it wants to be accessed by the outside world, and this works with the <servlet> element and < in the. xml file The servlet-mapping> element is complete.
The <servlet> element is used to register a servlet, which contains two primary child elements:<servlet-name> and <servlet-class> Used to set the registration name of the servlet and the full class name of the servlet, respectively.
An <servlet-mapping> element is used to map an external access path to a registered servlet, which contains two child elements:<servlet-name> and <url-pattern> Used to specify the name of the servlet's registry and the external access path of the servlet, respectively. For example:

1   <servlet>2     <servlet-name>servletdemo1</servlet-name>3     <servlet-class> Gacl.servlet.study.servletdemo1</servlet-class>4   </servlet>5 6   <servlet-mapping>7     <servlet-name>servletdemo1</servlet-name>8     <url-pattern>/servlet/servletdemo1</ Url-pattern>9   </servlet-mapping>

The same servlet can be mapped to multiple URLs, that is, the set value of the <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet. For example:

1 <servlet> 2 <servlet-name>ServletDemo1</servlet-name> 3 <servlet-class>gacl.servlet.study.ServletDemo1</servlet-class> 4 </servlet& Gt 5 6 <servlet-mapping> 7 <servlet-name>ServletDemo1</servlet-name> 8 <url-pattern>/servlet/servletdemo1</url-pattern> 9 </servlet-mapping>10 <servlet-mapping>11<Servlet-name>ServletDemo1</servlet-name>12 <url-pattern>/1.htm</url-pattern>13 </servlet-mapping>14 <servlet-mapping>15 <servlet-name>ServletDemo1</servlet-name>16 <url-pattern>/2.jsp</url-pattern>17 </servlet-mapping>18 <servlet-mapping>19 <servlet-name>ServletDemo1</servlet-name>20 <url-pattern>/3.php</url-pattern>21 </servlet-mapping>22 <servlet-mapping>23 <servlet-name>ServletDemo1</servlet-name>24 <url-pattern>/4.aspx</url-pattern>25 </servlet-mapping>

With the above configuration, when we want to access a servlet with the name ServletDemo1, we can use several addresses to access:

http://localhost:8080/JavaWeb_Servlet_Study_20140531/servlet/servletdemo1

http://localhost:8080/JavaWeb_Servlet_Study_20140531/1.htm

http://localhost:8080/JavaWeb_Servlet_Study_20140531/2.jsp

http://localhost:8080/JavaWeb_Servlet_Study_20140531/3.php

http://localhost:8080/JavaWeb_Servlet_Study_20140531/4.aspx

The SERVLETDEMO1 is mapped to multiple URLs.

5.2. servlet access URL using * wildcard mapping

You can also use the * wildcard character in the URL that the servlet maps to, but there can be only two fixed formats: one format is "*. Extension" and the other is preceded by a forward slash (/) and ends with "/*" . For example:

  

1  <servlet>2     <servlet-name>servletdemo1</servlet-name>3     <servlet-class> Gacl.servlet.study.servletdemo1</servlet-class>4   </servlet>5 6    <servlet-mapping>7     <servlet-name>servletdemo1</servlet-name>8    <url-pattern>/*</url-pattern>

* can match any character, so at this point you can use any URL to access the ServletDemo1 servlet, as shown in:

  

For some of the following mapping relationships:
Servlet1 Mapping to/abc/*
Servlet2 Map to/*
Servlet3 Mapping to/ABC
Servlet4 Mapping to *.do
Problem:
When the request URL is "/abc/a.html", "/abc/*" and "/*" match, which Servlet responds
The servlet engine calls Servlet1.
When the request URL is "/ABC", both "/abc/*" and "/ABC" match, which Servlet responds
The servlet engine calls Servlet3.
When the request URL is "/abc/a.do", both "/abc/*" and "*.do" match, which Servlet responds
The servlet engine calls Servlet1.
When the request URL is "/a.do", "/*" and "*.do" match, which Servlet responds
The servlet engine calls Servlet2.
When the request URL is "/xxx/yyy/a.do", "/*" and "*.do" match, which Servlet responds
The servlet engine calls Servlet2.
  The principle of matching is, "who looks more like who's looking?"

5.3. The difference between servlet and common Java class

The

Servlet is a Java class that is called by other Java programs (servlet engines) and cannot run independently, and it is run entirely by the servlet engine to control and dispatch.
multiple servlet requests for the client, typically, the server creates only one Servlet instance object, that is, once the Servlet instance object is created, it resides in memory and serves subsequent requests until the Web container exits. The servlet instance object will not be destroyed.
The servlet's Init method is called only once throughout the servlet's life cycle. Each access request to a servlet causes the servlet engine to invoke the Servlet's service method once. For each access request, the Servlet engine creates a new HttpServletRequest request object and a new HttpServletResponse response object. The two objects are then passed as arguments to the service () method of the servlet that it invokes, and the service method calls the Doxxx method separately, depending on the request.

If a <load-on-startup> element is configured in the <servlet> element, the Web application loads and creates an instance object of the servlet at startup, and the Init () method that invokes the Servlet instance object.
Example:
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
Org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Purpose: Write a initservlet for the Web application, which is configured to mount at boot time, creating the necessary database tables and data for the entire Web application.

5.4. Default servlet

If a servlet's mapping path is just a forward slash (/), the servlet becomes the default servlet for the current Web application.
The URL of a matching <servlet-mapping> element cannot be found in the Web. xml file, and their access requests are given to the default servlet processing, that is, the default servlet is used to handle access requests that are not handled by all other servlets. For example:

1  <servlet> 2     <servlet-name>ServletDemo2</servlet-name> 3     <servlet-class> Gacl.servlet.study.servletdemo2</servlet-class> 4     <load-on-startup>1</load-on-startup> 5   </servlet> 6    7   <!--Configure ServletDemo2 as the default servlet--8   <servlet-mapping> 9     < SERVLET-NAME>SERVLETDEMO2</SERVLET-NAME>10     <url-pattern>/</url-pattern>11   </servlet-mapping>

When accessing a servlet that does not exist, it is processed using the configured default Servlet, as shown in:

  

installation directory in <tomcat >\conf\ Web. xml file, a servlet named Org.apache.catalina.servlets.DefaultServlet is registered, and the servlet is set to the default servlet.

1     <servlet> 2         <servlet-name>default</servlet-name> 3         <servlet-class >org.apache.catalina.servlets.DefaultServlet</servlet-class> 4         <init-param> 5             <param-name>debug</param-name> 6             <param-value>0</param-value> 7         </init-param > 8         <init-param> 9             <param-name>listings</param-name>10             <param-value> False</param-value>11         </init-param>12         <load-on-startup>1</load-on-startup>13     </servlet>14  <!--the mapping for the default servlet-->16     <servlet-mapping>17< c17/><servlet-name>default</servlet-name>18         <url-pattern>/</ url-pattern>19     </servlet-mapping>

When accessing a static HTML file and picture in a Tomcat server, you are actually accessing the default servlet.

The configuration of the Tomcat file path Servelet and the path to Web-inf in Maven

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.