通用的Servlet的BaseServlet讲解

第一种:

首先create一个BaseServlet,并实现它的service方法.

通用的Servlet的BaseServlet讲解_第1张图片

BaseServlet的代码如下.

package com.itheima.web.servlet.base;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 通用的servlet
 * @author Admin
 */

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,HttpServletRequest.class);
			
			//3.让方法执行,接收返回值
			String path = (String) method.invoke(this, request,response);
			
			//4.判断返回值是否为空 若不为空统一处理请求转发
			if(path!=null){
				request.getRequestDispatcher(path).forward(request, response);
			}
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	public String index(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("text/html;charset=utf-8");			//设置中文编码
		response.getWriter().println("亲,不要捣乱!");
		return null;
	}
	
}

下面就是UserServlet继承BaseServlet.

package com.itheima.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itheima.web.servlet.base.BaseServlet;
	/**
	 * 用户模块
	 */
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;	
}

 

 

第二种:

首先创建一个BaseServlet的类,让其继承HttpServlet,代码如下.

package cn.com.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * BaseServlet用来作为其他Servlet的父类
 * @author Admin
 *
 *一个类多个请求处理方法,每个请求处理方法的原型与service相同!
 *原型 = 返回值类型 + 方法名称 + 参数列表
 */
public class BaseServlet extends HttpServlet {
		
		//重写Service方法
		@Override
		protected void service(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			/**
			 * 1.获取method参数,它是用户想调用的方法
			 * 2.把方法名称变成Method类的实例对象
			 * 3.通过invoke()来调用这个方法
			 */
			String methodName = request.getParameter("method");
			Method method = null;
			/**
			 * 2.通过方法名称获取Method对象
			 */
			try {
				method = this.getClass().getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException("您要调用的方法:"+methodName+"它不存在!",e);
			} 
			
			/**
			 * 3.通过method对象来调用它
			 */
			try {
				method.invoke(this, request,response);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
}

再创建一个AServlet使其继承BaseServlet,代码如下.

package cn.com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AServlet extends BaseServlet {

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("add()...");
	}
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("update()...");
	}
	
}

运行tomcat,访问页面的地址.

控制台会打印出add()...

 

进一步加强:

修改BaseServlet代码.

package cn.com.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * BaseServlet用来作为其他Servlet的父类
 * @author Admin
 *
 *一个类多个请求处理方法,每个请求处理方法的原型与service相同!
 *原型 = 返回值类型 + 方法名称 + 参数列表
 */
public class BaseServlet extends HttpServlet {
		
		//重写Service方法
		@Override
		protected void service(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			/**
			 * 1.获取method参数,它是用户想调用的方法
			 * 2.把方法名称变成Method类的实例对象
			 * 3.通过invoke()来调用这个方法
			 */
			String methodName = request.getParameter("method");
			Method method = null;
			/**
			 * 2.通过方法名称获取Method对象
			 */
			try {
				method = this.getClass().getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException("您要调用的方法:"+methodName+"它不存在!",e);
			} 
			
			/**
			 * 3.通过method对象来调用它
			 */
			try {
				String result = (String)method.invoke(this, request,response);
				if(result !=null && !result.trim().isEmpty()){
					String[] strs = result.split(":");
					if (strs[0].equals("r")) {
						request.getRequestDispatcher(strs[1]).forward(request, response);
					}else if (strs[0].equals("f")) {
						response.sendRedirect(request.getContextPath()+ strs[1]);
					}
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
}

修改AServlet.

package cn.com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AServlet extends BaseServlet {

	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("add()...");
		//request.getRequestDispatcher("/index.jsp").forward(request, response);
		return "f:/index.jsp";
	}

	public String update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("update()...");
		return "r:/index.jsp";
	}
}

然后通过网页访问,当前的add方法.

 

 

 

你可能感兴趣的:(§,--,JavaEE)