[Servlet & amp; JSP] deployment description settings

Source: Internet
Author: User
Tags error status code

[Servlet & JSP] deployment description settings
Servlet settings

The following is an example of servlet configuration in web. xml:

<servlet>    <servlet-name>Some</servlet-name>    <servlet-class>club.chuxing.SomeServlet</servlet-class>    <load-on-startup>1</load-on-startup>    <init-param>        <param-name>param1</param-name>        <param-value>value1</param-value>    </init-param></servlet><servlet-mapping>    <servlet-name>Some</servlet-name>    <url-pattern>/Some.do</url-pattern></servlet-mapping>

When the web application is started, it does not actually load all servlets. The container loads, instantiates, and carries out the initial action of the corresponding Servlet class during the request, and then calls the service () method to process the request.

If you want to load, instantiate, and initialize the Servlet class when the application starts, you can use . Set a value greater than 0 to initialize the Servlet after the application starts. The number indicates the Servlet initialization order. If multiple servlets are set When the same number is used, the Servlet is initialized in the order set in web. xml.

Do not set it on all servlets . The more servlets are initialized when the application starts, the more time the application starts. It is set only on servlets that will load more resources at the beginning. Such as the Servlet serving as the Front Controller in the web Framework.

You can Set three modes: Full Mode
It must start with a slash (/), and start with the root directory of the web application environment, specifying the complete URL mode. For example:

<url-pattern>/admin/login.do</url-pattern>


If you specify that all URLs in a directory are processed by a Servlet, you can add an asterisk (*) after the directory slash (*). For example

<url-pattern>/guest/*</url-pattern>


If all URL requests ending with an extension are processed by the corresponding Servlet, you can start with an asterisk (*) and add the extension.

<url-pattern>*.do</url-pattern>

You cannot use both the directory compliance mode and the extension compliance mode. /Admin/*. do Set the URL mode.

If the matching rules set in the URL mode overlap some URL requests, the comparison principle is determined by the order of the full mode, directory mode, and extension mode, that is, it starts from the strictest mode.

When you set a filter,<filter-mapping>You can also set<url-pattern>Label, setting principles and<servlet-mapping>On<url-pattern>Is the same. After Servlet2.5<servlet-mapping>Multiple<url-pattern>. For example:

<servlet-mapping>    <servlet-name>Some</servlet-name>    <utl-pattern>/something.do</utl-pattern>    <utl-pattern>/someone.do</utl-pattern></servlet-mapping>

You can also Multiple Or . For example:

<filter-mapping>    <filter-name>Some</filter-name>    <utl-pattern>/something.do</utl-pattern>    <utl-pattern>/someone.do</utl-pattern>    <servlet-name>Other</servlet-name></filter-mapping>
Set welcome and error handling page

If a user requests a URL similar to/BookmarkOnline/user, but the user does not exist in the Servlet URL mode setting, it is just a directory, if you do not want to see errors such as resource does not exist, you can set the default welcome page. When you request a directory, You can automatically select the welcome page to display it.

<web-app ...>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>

Set Note that the delimiter starts with a slash (/) (this is not the default homepage for the entire application ). When a directory name is requested, the container automatically In the file order, you can find the corresponding file, if found, it is displayed to the user.

If an exception or error occurs while accessing the application, but this exception or error is not handled in Servlet/JSP, the container will finally process it, generally, containers directly display exception information and stack trace information. If you want the container to automatically forward such exceptions or errors to a URL, you can use .

For example, if you want to forward a type of exception object when the container receives it, you can make the following settings:

     
          
   
    java.lang.NullPointerException
           
   
    /report.view
       
  
 

If you want to forward HTTP-based error codes to the processing page . For example:

<web-app ...>    <error-page>        <exception-type>java.lang.NullPointerException</exception-type>        <location>/report.view</location>    </error-page></web-app>

This setting also works when you use HttpServletResponse's sendError () to send an error status code, because sendError () only notifies the container that the container uses the default method of the container or web. xml to generate error status codes.

MIME and extension

When the stream is automatically responded to the browser, the MIME type must be informed to let the browser know how to process the received Stream object. If the MIME type is used in the application, you can set the extension to correspond to the MIMIE type in web. xml. For example:

<web-app ...>    <error-page>        <error-code>404</error-code>        <location>/404.jsp</location>    </error-page></web-app>

Set the file extension, while Set the MIME type name. If you want to know the MIME type name of a file, you can use the getMimeType () method of ServletContext. This method allows you to specify the file name based on the web. get the MIME type name for the corresponding extension of the total xml settings.

Related Article

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.