In the Servlet configuration file, you can use one or more <init-param> tags to configure some initialization parameters for the servlet. When the servlet is configured with initialization parameters, the web container will automatically encapsulate these initialization parameters into the ServletConfig object when creating the servlet instance object, and when calling the servlet's init method, the ServletConfig object will be passed to the servlet. Furthermore, programmers can obtain the initialization parameter information of the current servlet through the ServletConfig object.
The sample code is as follows:
Copy the code code as follows:
package com.yyz.servletconfig;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo1 extends HttpServlet {
ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get the specified initialization parameters
String value = config.getInitParameter("xxx");
response.getOutputStream().write(value.getBytes());
//Get all initialization parameters
Enumeration e = cofig.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
value = config.getInitParameter(name);
response.getOutputStream().write((name+"="+value+"<br/>").getBytes());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
The corresponding web.xml is as follows:
Copy the code code as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>ServletConfigDemo1</servlet-name>
<servlet-class>com.yyz.servletconfig.ServletConfigDemo1</servlet-class>
<init-param>
<param-name>xxx</param-name>
<param-value>yyy</param-value>
</init-param>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>yyy</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigDemo1</servlet-name>
<url-pattern>/servlet/ServletConfigDemo1</url-pattern>
</servlet-mapping>
</web-app>
The test results are as follows:
In the above code, there is a ServletConfig object in the ServletConfigDemo1 object, which is actually unnecessary. Because ServletConfigDemo1 inherits HttpServlet, which in turn inherits GenericServlet. GenericServlet already maintains a ServletConfig object internally. The relevant implementations are as follows:
Copy the code code as follows:
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable
{
… …
private transient ServletConfig config;
public ServletConfig getServletConfig() {
return config;
}
}
Therefore, we can directly get the ServletConfig object through the getServletConfig() method of the Servlet object we wrote. The sample code is as follows:
Copy the code code as follows:
package com.yyz.servletconfig;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = this.getServletConfig().getInitParameter("name");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml file:
Copy the code code as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>ServletConfigDemo2</servlet-name>
<servlet-class>com.yyz.servletconfig.ServletConfigDemo2</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigDemo2</servlet-name>
<url-pattern>/servlet/ServletConfigDemo2</url-pattern>
</servlet-mapping>
</web-app>