package com.ligong.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//缺省适配器设计模式
public abstract class GenericServlet implements Servlet,ServletConfig{
private ServletConfig config;
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return this.config;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig config) throws ServletException {
this.config=config;
}
//让子类来实现
```java
public abstract void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException;
public String getInitParameter(String name) {
// TODO Auto-generated method stub
return config.getInitParameter(name);
}
public Enumeration getInitParameterNames() {
// TODO Auto-generated method stub
return config.getInitParameterNames();
}
public ServletContext getServletContext() {
// TODO Auto-generated method stub
return config.getServletContext();
}
public String getServletName() {
// TODO Auto-generated method stub
return config.getServletName();
}
}
子类:
package com.ligong.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SomeServlet extends GenericServlet {
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("执行SomeServlet的service()服务");
String name=this.getInitParameter("name");
System.out.println("name:"+name);
}
@Override
public void init(ServletConfig config) throws ServletException {//该行config是来自服务器的
// TODO Auto-generated method stub
super.init(config);
}
}
如果子类要重写父类的init方法,那么必学显示的super 来自父类的init,不过不显示的super父类的init方法,那么当子类实例化话调用父类中设计到引用config对象所调用方法的时候,肯定会出现空指针异常,而且报错方式肯定是来自父类。
编译时期this关键字指向的是当前类创建的对象,
在运行时期this关键指向的就是子类创建的对象。
父类:
子类
只是,没有重写,也不需要进行重写。,所以不会执行无参数的init
那么当代码执行到了继承的父类的init方法的时候,其实这个时候的对象是Someservlet
那么所以
this.init();所指向的是子类中的无参初始化方法,而不是父类的无参构造方法。
模版方法设计模式:
父类:
package com.ligong.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//缺省适配器设计模式
public abstract class GenericServlet implements Servlet,ServletConfig{
private ServletConfig config;
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return this.config;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig config) throws ServletException {
this.config=config;
this.init();//编译期间,这个方法确实调用的是当前你类中的无参init();
}
public void init() {
System.out.println("无参数的初始化");
}
//让子类来实现
public abstract void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException;
public String getInitParameter(String name) {
// TODO Auto-generated method stub
return config.getInitParameter(name);
}
public Enumeration getInitParameterNames() {
// TODO Auto-generated method stub
return config.getInitParameterNames();
}
public ServletContext getServletContext() {
// TODO Auto-generated method stub
return config.getServletContext();
}
public String getServletName() {
// TODO Auto-generated method stub
return config.getServletName();
}
}
子类:
package com.ligong.servlets;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SomeServlet extends GenericServlet {
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("执行SomeServlet的service()服务");
String name=this.getInitParameter("name");
System.out.println("name:"+name);
}
/*@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
}*/
@Override
public void init() {
System.out.println("======");
}
}