Javaweb Foundation: ServletConfig

Source: Internet
Author: User

Basic concepts
    • ServletConfig is used to configure parameters for the servlet: in the servlet configuration file, you can configure some initialization parameters for the servlet using one or more <init-param> tags. When some servlet parameters need to be flexible and configurable, they can be implemented with ServletConfig.
    • When these initialization parameters are configured, Tomcat automatically encapsulates these parameters into servletconfig when creating the Servlet instance object, and passes the ServletConfig object to the servlet when invoking the servlet's initialization method.
    • Developers can obtain parameter information for the current servlet initialization through the ServletConfig object.
    • ServletConfig can be used to configure the path of the servlet's encoding Properties/database connection Properties/configuration file because it is configured for a single servlet and is not often used in real-world development.

Example: Configuring initialization parameters for Firstservletconfig

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version= "3.0">    <servlet>        <Servlet-name>Firstservletconfig</Servlet-name>        <Servlet-class>Servletconfig.firstservletconfig</Servlet-class>        <!--Init-param key-value Paris can be passed to the servlet via ServletConfig -        <Init-param>            <Param-name>Testservletconfig</Param-name>            <Param-value>Getfirstservletconfig</Param-value>        </Init-param>    </servlet>    <servlet-mapping>        <Servlet-name>Firstservletconfig</Servlet-name>        <Url-pattern>/firstservletconfig</Url-pattern>    </servlet-mapping>        <servlet>        <Servlet-name>Overrideservletconfig</Servlet-name>        <Servlet-class>Servletconfig.overrideservletconfig</Servlet-class>        <!--Init-param key-value Paris can be passed to the servlet via ServletConfig -        <Init-param>            <Param-name>Overrideservletconfig</Param-name>            <Param-value>Overrideservletconfig</Param-value>        </Init-param>    </servlet>    <servlet-mapping>        <Servlet-name>Overrideservletconfig</Servlet-name>        <Url-pattern>/overrideservletconfig</Url-pattern>    </servlet-mapping></Web-app>

Example: Get the ServletConfig object passed in by This.getservletconfig ()

Package Servletconfig;import Java.util.enumeration;import Javax.servlet.servletconfig;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/* * 1. Configure <init-param> * 2 under the servlet in Web-xml. Get the ServletConfig object: This.getservletconfig () or Declare servletconfig config, and then override the initialization method to invoke the Config object directly. (second choice one, cannot coexist) * 3. Obtain a specific initialization parameter by Config.getinitparameter (name) or Config.getinitparameternames () to get all initialization parameter names * */public class Firstservletconfig extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response) { Enumservletconfiginitparams ();} public void Enumservletconfiginitparams () {//This.getservletconfig () Gets the ServletConfig object passed by Tomcat enumeration< string> Initparas = This.getservletconfig (). Getinitparameternames (); String name, Value;while (Initparas.hasmoreelements ()) {name = Initparas.nextelement (); value = This.getservletconfig () . Getinitparameter (name); System.out.println ("Name:" + name); System.out.prIntln ("Value:" + value);}}} 

Example: Get the ServletConfig object passed in by overloading the Servlet initialization method

Package Servletconfig;import Javax.servlet.servletconfig;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Overrideservletconfig extends httpservlet{private servletconfig config;public void doget (HttpServletRequest request, HttpServletResponse response) {getservletconfiginitparambyname ();} public void Getservletconfiginitparambyname () {//Gets the initialization parameter value by entering a specific initialization parameter name string value = This.config.getInitParameter (" Overrideservletconfig "); System.out.println ("Value:" + value);} The servlet initialization method is overridden to pass the server encapsulated servletconfig to the private config object of the current servlet//Once this method is overridden, the this.getservletconfig () cannot be called again Gets the ServletConfig object of the current servlet, which can only be manipulated by passing the Private variable CONFIG. public void init (servletconfig config) {this.config=config;}}

  

Javaweb Base: ServletConfig

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.