JAVA从入门到进阶(十四)——Servlet学习和手写剖析GenericServlet源码

Servlet类实现方式
1.实现 Servlet接口 2 继承GenericServlet类 3.继承HttpServlet类
Servlet作用:
1接受请求
2处理请求
3完成响应

Servlet接口
Servlet中的方法大多数不需要我们来调用,而是由服务器(Tomcat)来调用。
其中有生命周期方法
void init(Servlet Config) :会被调用一次 在对象初始化后立即调用此方法
void destory():会被调用一次 在Servlet对象销毁前被调用 常用于释放资源
void service():会被调用多次 在每次进行服务时都会进行调用此方法
其他方法
ServletConfig getServletConfig():获取Servlet配置信息
String getServletInfo():获取Servlet信息
特点:Servlet类由我们来进行创建,但Servlet对象是由服务器来创建
每个Servlet对象是单例的,可能存在多个Servlet类。每个类用来处理不同的请求。当有多个用户来访问Servlet时,是线程不安全的

二GenericServlet类
GenericServlet是Servlet的实现类方法有

Method Summary
 void	destroy() 
          Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
 java.lang.String	getInitParameter(java.lang.String name) 
          Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
 java.util.Enumeration	getInitParameterNames() 
          Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
 ServletConfig	getServletConfig() 
          Returns this servlet's ServletConfig object.
 ServletContext	getServletContext() 
          Returns a reference to the ServletContext in which this servlet is running.
 java.lang.String	getServletInfo() 
          Returns information about the servlet, such as author, version, and copyright.
 java.lang.String	getServletName() 
          Returns the name of this servlet instance.
 void	init() 
          A convenience method which can be overridden so that there's no need to call super.init(config).
 void	init(ServletConfig config) 
          Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
 void	log(java.lang.String msg) 
          Writes the specified message to a servlet log file, prepended by the servlet's name.
 void	log(java.lang.String message, java.lang.Throwable t) 
          Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file, prepended by the servlet's name.
abstract  void	service(ServletRequest req, ServletResponse res) 
          Called by the servlet container to allow the servlet to respond to a request.

下面进行模拟GennericServlet类的实现

public abstract  class MyGenericdemo1 implements Servlet{

	private ServletConfig config;
	public void destroy() {
		System.out.println("我被销毁了");
		
	}
	//会在init后调用 ,此时config已经存在类中了
	public ServletConfig getServletConfig() {
		return this.config;
	}
	public String getServletInfo() {
		return null;
	}
	//init方法在构造器后第一个进行调用,由服务器来传入ServletConfig对象
	//我们可以在类中定义一个ServletConfig对象的引用对服务器传入的对象进行捕捉
	public void init(ServletConfig servletConfig) throws ServletException {
		
		this.config=servletConfig;
		init();
	}
	//如果子类进行对init(ServletConfig servletConfig)进行覆盖 那么config对象将捕捉不到
	//我们可以使用里面搭配一个无参的init()方法,在子类只需要重写无参的init()即可
	public void init()
	{
		
	}
	//可以把服务定义为抽象类 子类进行可以对Service进行重写
	public abstract void service(ServletRequest arg0, ServletResponse arg1)
			throws ServletException, IOException;
	public String	getInitParameter(String string) 
	{
		return this.config.getInitParameter(string);
	}
	public Enumeration	getInitParameterNames() 
	{
		return this.config.getInitParameterNames();
	}
	/*
	 * void	log(java.lang.String msg) 
          Writes the specified message to a servlet log file, prepended
        by the servlet's name.
        void	log(java.lang.String message, java.lang.Throwable t) 
          Writes an explanatory message and a stack trace for a given Throwable exception to the servle
          t log file, prepended by the servlet's 
	 */
	
}

重点:在模拟的GenericServlet中,通过在带参的ini方法t中 调用一个无参的init方法,这样子类在覆盖时,就不会把父类中的信息丢失,而且带参数的init方法在servlet初始化时被调用,当子类覆盖了无参的init方法,自然也直接会调用子类的init方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值