若是咱們直接經過實現Servlet接口來編寫一個Servlet類,就須要實現Servlet接口定義的5種方法,爲了簡化Servlet的編寫,在javax.servlet包中提供了一個抽象類GenericServlet,該類提供了除service()方法外的其餘4種方法的簡單實現。GenericServlet類定義了一個通用的、不依賴於具體協議的Servlet,它實現了Servlet接口和ServletConfig接口。java
public abstract class GenericServlet implements Servlet, ServletConfig
下列表中羅列了GenericServlet的全部方法:web
Method Summary | |
---|---|
void | destroy() |
ServletConfig | getServletConfig() |
ServletContext | getServletContext() |
void | init() |
void | init(ServletConfig config) |
abstract void | service(ServletRequest req, ServletResponse res) |
若是咱們要自定義一個通用的Servlet,只須要從GenericServlet類繼承,並實現其中的抽象方法service()方法。apache
在GenericServlet類中,提供了兩種重載的init()方法:markdown
第一種init()方法是Servlet接口中init()方法的實現。在這種方法中,首先將ServletConfig對象保存在一個transient實例變量中,而後調用第二種不帶參數的init()方法。app
一般咱們在編寫繼承於GenericServlet的自定義Serlvet時,只須要重寫第二中不帶參數的init()方法就能夠了。框架
在javax.servlet包中,定義了ServletConfig接口。Servlet容器使用ServletConfig對象在Servlet初始化時向其傳遞配置信息。jsp
所謂的Serlvet配置信息,就是在Web應用程序中web.xml文件中配置有關Servlet的內容。ide
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>app.java.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet/FirstServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
要想從ServletConfig對象獲取有關Servlet的配置信息,首先須要得到ServletConfig對象,方式有如下幾種:學習
public class ConfigServlet extends GenericServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
}
}
ServletConfig servletConfig = getServletConfig();
ServletConfig接口提供瞭如下方法供使用:this
Method Summary | |
---|---|
String | getInitParameter(String name) |
Enumeration | getInitParameterNames() |
ServletContext | getServletContext() |
String | getServletName() |
下面咱們一一來討論上述方法的做用:
<servlet>
<servlet-name>ConfigServlet</servlet-name>
<servlet-class>app.java.servlet.ConfigServlet</servlet-class>
</servlet>
咱們經過獲取到的ServletConfig對象調用getServletName()方法來驗證。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();
String servletName = servletConfig.getServletName();
System.out.println(servletName);
}
運行Web應用程序,在控制檯中打印「ConfigServlet」。
首先,咱們須要在web.xml文件中有關Servlet配置信息中,增長初始化參數的內容。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<servlet>
<servlet-name>ConfigServlet</servlet-name>
<servlet-class>app.java.servlet.ConfigServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>jyl</param-value>
</init-param>
<init-param>
<param-name>blog</param-name>
<param-value>http://www.longestory.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigServlet</servlet-name>
<url-pattern>/servlet/ConfigServlet</url-pattern>
</servlet-mapping>
</web-app>
而後,咱們在自定義Servlet的service()方法中獲取對應初始化參數。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();
String name = servletConfig.getInitParameter("name");
String blog = servletConfig.getInitParameter("blog");
System.out.println(name + "'s blog is " + blog);
}
運行Web應用程序,在控制檯中打印「jyl’s blog is http://www.longestory.com」。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();
Enumeration enumeration = servletConfig.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String element = (String) enumeration.nextElement();
String value = servletConfig.getInitParameter(element);
System.out.println(element + ": " + value);
}
}
運行Web應用程序,在控制檯中打印:
name: jyl
blog: http://www.longestory.com
ServletConfig有哪些實際做用呢?在struts 1 框架中就運行了ServletConfig內容。
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml,
/WEB-INF/struts-config-Wildcard.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
轉載說明:請註明做者及原文連接,謝謝!