Configure Web.xml files for struts application

Source: Internet
Author: User
Tags error code error handling java web

Web.xml files are necessary for configuring any Java Web application. When you configure the struts application, you should also configure the struts-related configuration options in the Web.xml file. Describe the necessary steps to configure struts application.

1 • Configure the Actionservlet of struts

The first step is to declare actionservlet with <servlet> elements.

Among the child elements of <servlet>, what is often used is:

The <servlet-name> element is used to define the name of the servlet.

The <servlet-class> element is used to top the full class name of the servlet.

The <init-param> element is used to configure the servlet initialization parameters.

The second step is to configure <servlet-mapping> element to specify which URLs Actionservlet can process.

Have child elements <servlet-name> and <url-pattern>, note that here <servlet-name> must and <servlet> elements in the <servlet-name > Match.

* * Hint * *

No matter how many child applications are included in the application, you only need to configure a actionservlet. Some developers want to set up multiple Actionservlet classes to handle different functions in the application, which is unnecessary because the Serlvet itself supports multithreading. Also, the current struts framework allows only one actionserlvet to be configured in the application.

2 • Configuration Welcome file list

The <welcome-file-list> element can contain multiple <welcome-file> child elements, and when the Web container invokes the Web application's welcome file, it first looks for the first <welcome-file>-specified file. If this file is present, the file will be returned to the customer, and if the file does not exist, the Web container will search for the next welcome file in turn until it is found, and if none exists, the server will return an "HTTP 404 Not Found" error message to the customer.

3 • Configuration error handling

<error-page>
<error-code></error-code>
<location></location>
</error-page>

If the above configuration is done in the Web.xml file, when the Web container catches an error (for example, HTTP 404, http 500), the child element of <error-page> is reduced according to the error code <error-code> Returns the file specified by the <location> child element if there is a match.

The Java exception configuration <error-page> element that can also be captured for web containers is the need to set <exception-type> child elements that specify the Java exception class.

The Web container might catch the following exceptions:

· RuntimeException or error.

· Servletexception or its subclasses.

· IOException or its subclasses.

The configuration information here is as follows:

<error-page>
<exception-type></exception-type>
<location></location>
</error-page>

4 • Configure Struts Tag Library

The <taglib> element has two child elements:<taglib-uri> and <taglib-location>. <taglib-uri> element specifies the relative or absolute URI address of the tag library in which the Web application accesses the tag library;<taglib-location> element to specify the physical location of the tag library description file in the file resource system.

If the Struts tag library is not used in a Web application, there is no need to configure it in the Web.xml file. In addition, you can configure user-defined custom tag libraries in the Web.xml file in the above manner.

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.