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>