Getting Started with servlet development (5) Obtaining initialization configuration information

Source: Internet
Author: User

The initialization parameters to be obtained in servlet development are used with the Config object in the JSP built-in object, which is actually an interface instance of the ServletConfig object. This article explains the acquisition of the parameters of the servlet initialization. is to get a set of parameters when the servlet initializes.

The initialization method can be done using the init () method, as has been said in the previous article, that Init () is used to initialize a servlet program. In the Init () method, you can pass in an ServletConfig object, which is an operation that can accept initialization configuration parameters, because all initialization parameters must be configured in the Web.xml file.

The method for Init () is as follows:

public void init (servletconfig config)    
             throws servletexception{}

The following is an instance of getting the initialization parameter

Package Servletdemo;    
Import java.io.IOException;    
Import javax.servlet.*;    
Import javax.servlet.http.*;    
The public class Initservlet extends httpservlet{    
    private String initparam=null;//is used to receive initialization parameters public    
    void init ( ServletConfig config) throws servletexception{    
        This.initparam=config.getinitparameter ("ref");//Receive initialization Parameters    
    } Public    
    void Doget (HttpServletRequest req,httpservletresponse resp)    
               throws servletexception,ioexception{     
        System.out.println ("Initialization parameter:" +this.initparam);//output initialization parameter    
}    
    public void DoPost (HttpServletRequest req,httpservletresponse resp)    
          throws servletexception,ioexception{// Process POST Request    
       This.doget (req, resp);   Call Doget () method           
}    
}

If a servlet is to work, it must be configured in the Web.xml file.

<servlet>    
       <servlet-name>initParameter</servlet-name>    
       <servlet-class> servletdemo.initservlet</servlet-class>    
       <init-param>    
             <param-name>ref</param-name >  //Parameter name    
             <param-value>http://zhaoyuqiang.blog.51cto.com</param-value>//parameter content    
       </ init-param>    
  </servlet>    
  <servlet-mapping>    
       <servlet-name>initparameter</ servlet-name>    
       <url-pattern>/initServletTest</url-pattern>    
  </servlet-mapping>

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.