ServletConfig
物件是由每個servlet的Web容器建立的。這個物件可用於從web.xml
檔案獲取組態資訊。
如果從web.xml
檔案修改組態資訊,就不需要更改servlet中的程式碼。因此,對於有特定組態內容需要不定時修改,這樣會更容易管理Web應用程式。
ServletConfig
的核心優點是,如果可以修改web.xml
檔案中的資訊,則不需要編輯servlet程式碼檔案。
ServletConfig介面的方法
編號 | 方法 | 描述 |
---|---|---|
1 | public String getInitParameter(String name) |
返回指定引數名稱的引數值。 |
2 | public Enumeration getInitParameterNames() |
返回所有初始化引數名稱的列舉。 |
3 | public String getServletName() |
返回servlet的名稱。 |
4 | public ServletContext getServletContext() |
返回ServletContext 的物件。 |
如何獲取ServletConfig的物件?
getServletConfig()
方法來返回ServletConfig
物件。getServletConfig()方法的語法
public ServletConfig getServletConfig();
getServletConfig()方法的範例
ServletConfig config=getServletConfig();
//Now we can call the methods of ServletConfig interface
為servlet提供初始化引數的語法
servlet的init-param
子元素用於指定servlet的初始化引數。
<web-app>
<servlet>
......
<init-param>
<param-name>parameter_name</param-name>
<param-value>parameter_value</param-value>
</init-param>
......
</servlet>
</web-app>
在這個例子中,將從web.xml
檔案獲取一個初始化引數,並在servlet中列印這些資訊。
建立一個名稱為:ServletConfig的動態Web應用專案,其結構如下所示 -
下面是幾個主要的程式碼檔案 -
檔案:ConfigServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConfigServlet
*/
public class ConfigServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletConfig config = getServletConfig();
String driver = config.getInitParameter("mysql_driver");
out.print("Driver details is: <b>" + driver+"</b>");
out.close();
}
}
檔案:web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ConfigServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ConfigServlet</servlet-name>
<servlet-class>com.yiibai.ConfigServlet</servlet-class>
<init-param>
<param-name>mysql_driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigServlet</servlet-name>
<url-pattern>/dbconfig</url-pattern>
</servlet-mapping>
</web-app>
將上面專案部署並存取URL: http://localhost:8080/ConfigServlet/dbconfig ,如果程式沒有問題,那麼應該會看到以下結果 -
在這個例子中,我們從web.xml
檔案獲取所有的初始化引數,並在servlet中列印這些資訊。
幾個主要程式碼檔案如下 -
檔案:ConfigServletAll.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
* Servlet implementation class ConfigServletAll
*/
public class ConfigServletAll extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ServletConfig config = getServletConfig();
Enumeration<String> e = config.getInitParameterNames();
String str = "";
while (e.hasMoreElements()) {
str = e.nextElement();
out.print("Name: " + str);
out.print(" value: " + config.getInitParameter(str)+"<br/>");
}
out.close();
}
}
檔案:web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ConfigServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ConfigServlet</servlet-name>
<servlet-class>com.yiibai.ConfigServlet</servlet-class>
<init-param>
<param-name>mysql_driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigServlet</servlet-name>
<url-pattern>/dbconfig</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ConfigServletAll</servlet-name>
<servlet-class>com.yiibai.ConfigServletAll</servlet-class>
<init-param>
<param-name>mssql_driver</param-name>
<param-value>com.mssql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>passwd</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigServletAll</servlet-name>
<url-pattern>/dbconfigall</url-pattern>
</servlet-mapping>
</web-app>
將上面專案部署並存取URL: http://localhost:8080/ConfigServlet/dbconfigall ,如果程式沒有問題,那麼應該會看到以下結果 -