在Servlet的設定檔中,可以使用一個或多個<init-param>標籤為servlet配置一些初始化參數。當servlet配置了初始化參數後,web容器在建立servlet實例物件時,會自動將這些初始化參數封裝到ServletConfig物件中,並在呼叫servlet的init方法時,ServletConfig物件傳遞給servlet。進而,程式設計師透過ServletConfig物件就可以得到目前servlet的初始化參數資訊。
範例程式碼如下:
複製代碼代碼如下:
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 {
//取得指定的初始化參數
String value = config.getInitParameter("xxx");
response.getOutputStream().write(value.getBytes());
//取得所有的初始化參數
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;
}
}
對應的web.xml如下:
複製代碼代碼如下:
<?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>
測試結果如下:
在上面的程式碼中,ServletConfigDemo1物件中有一個ServletConfig對象,其實這是不必要的。因為ServletConfigDemo1繼承了HttpServlet,HttpServlet又繼承了GenericServlet 。 GenericServlet 已經在內部維護了一個ServletConfig物件。相關實現如下:
複製代碼代碼如下:
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable
{
… …
private transient ServletConfig config;
public ServletConfig getServletConfig() {
return config;
}
}
因而我們可以透過我們寫的Servlet物件的getServletConfig()方法直接拿到ServletConfig對象,範例程式碼如下:
複製代碼代碼如下:
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檔:
複製代碼代碼如下:
<?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>