LinkinPark
当你的才华撑不起你野心时,那么请潜下心继续学习,心无旁骛,愿多年以后你们我都能成为自己想象的模样。
  • ServletConfig接口
定义:public interface ServletConfig
这个接口定义了一个对象, 通过这个对象, Servlet 引擎配置一个 Servlet 并且允许 Servlet获得一个有关它的 ServletContext 接口的说明。每一个 ServletConfig 对象对应着一个唯一的Servlet。
方法:
1、getInitParameter
public String getInitParameter(String name);
这个方法返回一个包含 Servlet 指定的初始化参数的 String。 如果这个参数不存在, 返加空值。
2、getInitParameterNames
public Enumeration getInitParameterNames();
这个方法返回一个列表 String 对象,该对象包括 Servlet 的所有初始化参数名。如果Servlet 没有初始化参数,getInitParameterNames 返回一个空的列表。
3、getServletContext
public ServletContext getServletContext();
返回这个 Servlet 的 ServletContext 对象。



  • GenericServlet接口
public abstract class GenericServlet implements Servlet,ServletConfig, Serializable;
这个类的存在使得编写 Servlet 更加方便。它提供了一个简单的方案,这个方案用来执
行有关Servlet生命周期的方法以及在初始化时对ServletConfig对象和ServletContext对象进
行说明。
方法
1、destroy
public void destroy();
在这里 destroy 方法不做任何其他的工作。
2、getInitParameter
public String getInitParameter(String name);
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
3、getInitParameterNames
public Enumeration getInitParameterNames();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
4、getServletConfig
public ServletConfig getServletConfig();
返回一个通过这个类的 init 方法产生的 ServletConfig 对象的说明。
5、getServletContext
public ServletContext getServletContext();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
6、getServletInfo
public String getServletInfo();
返回一个反映 Servlet 版本的 String。
7、init
public void init() throws ServletException;
public void init(ServletConfig config) throws ServletException;

init(ServletConfig config)方法是一个对这个 Servlet 的生命周期进行初始化的简便的途径。

init(ServletConfig config)方法会存储config对象然后调用init() 。 如果你重载了这个方法 ,你必须调用 super.init(config),这样 GenericServlet 类的其他方法才能正常工作。

init()方法是用来让你对 GenericServlet 类进行扩充的,使用这个方法时,你不需要存储config 对象,也不需要调用 super.init(config)。
8、 log
public void log(String msg);
public void log(String msg, Throwable cause);
通过 Servlet content 对象将 Servlet 的类名和给定的信息写入 log 文件中。
9、 service
public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;
这是一个抽象的方法,当你扩展这个类时,为了执行网络请求,你必须执行它。

package javax.servlet;

import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
{
	//这里封装一个ServletConfig对象,每一个 ServletConfig 对象对应着一个唯一的Servlet。
	private transient ServletConfig config;

	public void destroy()
	{
	}

	//这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。以下3个方法都是:
	public String getInitParameter(String name)
	{
		return getServletConfig().getInitParameter(name);
	}

	public Enumeration getInitParameterNames()
	{
		return getServletConfig().getInitParameterNames();
	}

	//Servlet引擎配置一个 Servlet,并且允许 Servlet,获得一个有关它的 ServletContext 接口的说明
	public ServletContext getServletContext()
	{
		return getServletConfig().getServletContext();
	}

	//我们常常在自己写的Servlet用这个方法,其实就是在这里被继承过去的
	public ServletConfig getServletConfig()
	{
		return this.config;
	}

	public String getServletInfo()
	{
		return "";
	}

	//重写Servlet的init方法
	public void init(ServletConfig config) throws ServletException
	{
		this.config = config;
		init();
	}

	public void init() throws ServletException
	{
	}

	public void log(String msg)
	{
		getServletContext().log(getServletName() + ": " + msg);
	}

	public void log(String message, Throwable t)
	{
		getServletContext().log(getServletName() + ": " + message, t);
	}

	//推迟到子类实现,这里仍然是抽象方法
	public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException, IOException;

	public String getServletName()
	{
		return this.config.getServletName();
	}
}

posted on 2015-06-14 22:16  LinkinPark  阅读(332)  评论(0编辑  收藏  举报