1.首先是BaseServlet
/**
* 通用的servlet
*/
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.获取方法名称
String mName = request.getParameter("method");
//1.1判断 参数是否为空 若为空,执行默认的方法
if(mName == null || mName.trim().length()==0){
mName = "index";
}
//2.获取方法对象
Method method = this.getClass().getMethod(mName, HttpServletRequest.class,HttpServletResponse.class);
//3.让方法执行,接受返回值
String path=(String) method.invoke(this, request,response);
//4.判断返回值是否为空 若不为空统一处理请求转发
if(path != null){
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}
2.然后是很多servlet,这里展示UserServlet,继承BaseServlet
/**
* 用户模块
*/
public class UserServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
/**
* 退出
*/
public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().invalidate();
response.sendRedirect(request.getContextPath());
return null;
}
/**
* 用户登录
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
}
/**
* 跳转到登录页面
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String loginUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return "/jsp/login.jsp";
}
/**
* 用户激活
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
}
/**
* 用户注册
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
}
/**
* 跳转到注册页面
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String registUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return "/jsp/register.jsp";
}
}
3.理解下这个BaseServlet
Servlet默认是在第一次被访问的时候被创建。
所以当我们通过web.xml的映射关系访问到UserServlet时,UserServlet对象被创建,当然作为父类会先通过构造函数来初始化。每次客户向服务器发送请求时,服务器就会调用service方法。
重写的service方法中:
通过String mName = request.getParameter("method")得到要调用的UserServlet的方法名;
通过Method method = this.getClass().getMethod(mName, HttpServletRequest.class,HttpServletResponse.class)得到方法对象,this是对象;
再通过method.invoke(this, request,response)来执行这个方法。
4.简单例子
父类 Fanshe 和两个子类 AServlet 和 BServlet
public class Fanshe {
Fanshe(){
System.out.println(this.getClass());
}
public void say(String s){
System.out.println("Fanshe say");
String methodName = "eat";
try {
Method method = this.getClass().getMethod(methodName, String.class);//第一个参数是方法名,第二个参数是方法的参数列表
method.invoke(this, s);//第一个参数是对象名,第二个参数是方法的参数列表
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AServlet a = new AServlet();
a.say("A");
Bservlet b = new Bservlet();
b.say("B");
}
}
public class AServlet extends Fanshe{
public void eat(String s){
System.out.println(s+" eat");
}
}
public class Bservlet extends Fanshe{
public void eat(String s){
System.out.println(s+" eat");
}
}
运行结果:
class AServlet
Fanshe say
A eat
class Bservlet
Fanshe say
B eat