Web.xml files are required for configuring any Java Web application.
(1) Configure the Actionservlet of struts
<servlet><!--element to declare actionservlet--> <servlet-name><!--elements: The name used to define the servlet--> < servlet-class><!--element: Used to specify the full class name of the servlet--> <servlet> <servlet-name>action</servlet-name > <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet>
Also configure the <servlet-mapping> element to specify which URLs Actionservlet can handle
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern > </servlet-mapping> Note: (1) <servlet-name>?</in <servlet-mapping> and <servlet> Servlet-name> to fill in the same, it is a line, the <servlet-mapping> and <servlet> joined together.
(2) There can be only one servlet in the Struts framework, because the servlet supports multithreading. and <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> The Actionservlet in the Struts.jar package is imported when the struts package is imported.
(3) A servlet will be used to submit the display layer as *.do. *.do can be written in any form you want. For example:/do/*
Extensions (1) We can inherit Org.apache.struts.action.ActionServlet to get the subclasses we can extend, overriding a method init () in the subclass. At this point in the <servlet-class>?</servlet-class>. is the path of our new class, and there can only be one.
<servlet> <servlet-name>action</servlet-name> <servlet-class> Org.apache.struts.action.actionservlet</servlet-class> <init-param> <param-name>config</ Param-name> <param-value>/WEB-INF/struts-config.xml</param-value>
Detailed description of the Web.xml file in Tomcat
<?xml version= "1.0" encoding= "GB2312"?> <!--web.xml in turn, the following elements are defined: <web-app> <display-name></ Display-name> defines the Web application's name <description></description> statement Web application description Information <filter></filter> <filter-mapping></filter-mapping> <servlet></servlet> <servlet-mapping></ Servlet-mapping> <session-config></session-config> <welcome-file-list></ Welcome-file-list> <taglib></taglib> <resource-ref></resource-ref> < Security-constraint></security-constraint> <login-config></login-config> </web-app> The sequence of elements defined in Web.xml cannot be reversed, or the Tomcat server may throw saxparseexception.--> <! DOCTYPE Web-app Public "-//sun MICROSYSTEMS,INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "> <web-app> <display-name>sample application</display-name> <description> This is a Sample Application </description> <!--filter configuration ServleThe T filter filter-name defines the name of the filter. When there are multiple filters, you cannot have the same name Filter-class specifies the class that implements this filter, which is responsible for the specific filtering transaction--> <filter> <filter-name>samplefilter</ Filter-name> <filter-class>mypack. Samplefilter</filter-class> </filter> <!--filter-mapping Settings filter is responsible for filtering the URL filter-name filter name. The name here must match the filter name in filter Url-pattern specifies the filter responsible for filtering the URL--> <filter-mapping> <filter-name>SampleFilter< /filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!--servlet configuration servlet. Servlet-name defines the name of the servlet servlet-class specifies that the class that implements this servlet Init-param define the initialization parameters and parameter values of the servlet, and can have multiple init-param. An initialization parameter is accessed through the Getinitparamenter (String name) method in the Servlet class Load-on-startup specifies the order in which the servlet is loaded when the Web application is started. The value is either positive or zero: the servlet container loads the servlet with a small number and then loads the other large servlet in turn. The value is negative or undefined: the servlet container will load it when the Web customer first accesses the servlet--> <servlet> <servlet-name>sampleservlet</ Servlet-name> <servlet-class>mypack. Sampleservlet</servlet-class> <init-param> <param-name>initparam1</param-name> <param-value>2</param-value> </init-param> <load-on-startup>1</ Load-on-startup> </servlet> <!--Configure the servlet mapping the relative URL specified by the following code for Sampleservlet is "/sample": Servlet-name Specifies the name of the Servlet, where the name should match the name defined in the <Servlet> element. Url-pattern Specifies the URL to access this servlet. Just give the relative path. --> <servlet-mapping> <servlet-name>SampleServlet</servlet-name> <url-pattern>/sample </url-pattern> </servlet-mapping> <!--configuration session,session is used to set the HttpSession lifecycle. Unit (SEC)--> <session-config> <session-timeout>30</session-timeout> </session-config> <! --Configure WEL0COME0 file list--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> < Welcome-file>index.htm</welcome-file> </welcome-file-list> <!--configuration tag Library Taglib-uri set Tag The unique identifier of the library in which the tag library taglib-location specifies the location of the TLD file corresponding to the tag library in the Web application--> <taglib> < Taglib-uri>/mytaglib</taglib-uri> <taglib-loCation>/web-inf/mytaglib.tld</taglib-location> </taglib> <!--Configure a resource reference description a description of the referenced resource RES-REF-NAME Specifies the Jndi name of the referenced resource Res-type specifies the class name of the referenced resource Res-auth specifies the manager that manages the referencing resource, which has two optional values: Container: Create and manage by container resource Application: Creating and Managing Resource--> <resource-ref> <description>db connection</description> with Web applications <res-ref-name>jdbc/sampleDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <!--Configure security constraints (the following code specifies that when a user accesses all resources under this Web application, Must have a guest role Web-resource-collection declares that a protected web resource Auth-constraint Declaration can access the role of a protected resource and can contain multiple <role-name> child elements Web-resource-name identifies protected Web resources url-pattern specify protected URL paths--> <Security-constraint> <web-resource-collection > <web-resource-name>sample appliction</web-resource-name> <url-pattern>/*</url-pattern > </web-resource-collection> <auth-constraint> <role-name>guest</role-name> </ Auth-coNstraint> </Security-constraint> <!--Configure the Security Authentication login interface: Specifies the type of logon dialog box that the system pops up when a Web client accesses a protected web resource. AUTH-METHOD Specifies the authentication method, which has three optional values: Basic (Basic authentication), DIGEST (Digest validation), form (form validation) Realm-name setting the name of the security domain Form-login-config when the authentication method is FORM, Configure validation pages and error pages Form-login-page when the authentication method is form, set the validation page form-error-page when the validation method is form, set the error page--> <login-config> < auth-method>form</auth-method> <realm-name> Tomcat Server Configuration form-based authentication Area </realm-name> <form-login-config> <form-login-page>/login.jsp</form-login-page> < Form-error-page>/error.jsp</form-error-page> </form-login-config> </login-config> <!-- Configure a reference to the security validation role--> <security-role> <description> the ' is ' required to log into the sample application </description> <role-name>guest</role-name> </security-role> </web-app>
(2) Declaring the initialization parameters of the Actionservlet
Initialization parameters are used to initially configure the servlet's run-time environment. The <init-param> child elements of <servlet> are used to configure the servlet initialization parameters.
Config: Indicates the configuration file location of the struts application as a relative path, if not set, the default value is/web-inf/struts-config.xml
Debug: Set the debug level of the servlet to control the verbosity of the logging. The default is 0, which records relatively minimal log information.
Detail: Set the debug level for Digester, Digester is a framework used by the struts framework to parse XML configuration files, and this setting allows you to view analytic logs of different levels of detail. The default is 0, and the record is relatively
Less log information.
<load-on-startup>. </load-on-startup> in. The value of the number is the order in which this actionservlet is loaded when the server is turned on, and the lower the value, the more it loads first.