servlet中反射应用

4 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍了Servlet中反射的应用,以BaseServlet为例,当UserServlet被访问时,通过反射调用相应的方法。在service方法中,获取请求参数以决定执行哪个方法,并通过Method对象的invoke方法执行。提供了一个简单的 Fanshe 类及 AServlet、BServlet 子类的例子,展示了反射如何在多继承servlet场景下工作。
摘要由CSDN通过智能技术生成

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值