The servlet has a ServletConfig object, a method that four config objects can call, a ServletContext object to get Web project information, and a configuration that sets the global initialization parameters __web

Source: Internet
Author: User

ServletConfig objects

each servlet has a ServletConfig object

ServletConfig The object used to obtain the related configuration of the servlet.

get ServletConfig object: Direct This.getservletconfig () To return config object directly

the method that four config objects can call:

 

Getinitparameter (String name); Gets the value of the parameter initialization by initializing the name

Getinitparameternames (); By this method, returns an enumeration of String generics, enumeration<string>; Inside is the name of all the initialization. Gets all of the corresponding values through traversal.

With the enumeration object. Call the hasMoreElements () method to know if there is a next element. Nextelement (); get the next element

Getservletcontext (); Get ServletContext object

getservletname (); Get the name of the configuration

Configure initialization parameters

Configure the name of the initialization parameter in the label <init-param> configuration and the corresponding value <param-name> and <param-value>

5 ServletContext object to get Web project information

Overview of ServletContext objects

What is ServletContext

The context object for the Servletcontext:servlet. The ServletContext object is known to the content before and after the servlet. This object has only one Web project . Create a separate ServletContext object for each Web project when the server is started.

The role of ServletContext objects

Function one: To get Web project information

because a Web project has only one ServletContext object, This object is known to be relevant to the entire project.

Method:

gets the object of the ServletContext returned by the ServletContext object through the This.getservletcontext ();

gets the MIME type of the file: GetMimeType (filename);

gets the project name of the Web project request: Getcontextpath ();

To set the configuration of global initialization parameters: Add <context-param> tags to web.xml and add <param-name> and <param-value> values to this tab

gets the initialization parameter in the Web project XML file: Getinitparameter ("parameter name") returns the value of the parameter

gets the full name parameter name of the file for the Web project xml: Getinitparameternames (), returns an enumeration of string generics, enumeration<string>; inside is the name of all initialization. Gets all of the corresponding values through traversal.

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.