アプリケーション反射サーブレット

package org.news.servlet;

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

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

public class BaseServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BaseServlet() {
		super();
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
	@Override
	protected void service(HttpServletRequest request,HttpServletResponse response){
		//创建java反射实现调用方法
		//不需要依赖实例化的对象就可以拿到所有类的资源
		try {			
			request.setCharacterEncoding("utf-8");		
			Class clazz=this.getClass();	
			String opr=	request.getParameter("opr");
			//this是拿到所有子类对象 		
			
			//一开始加载主页的时候没有参数,默认是加载全部显示数据
			if(opr==null){
				opr="list";
			}		
			//要找的方法的条件
			Method me=	clazz.getMethod(opr,
							HttpServletRequest.class,
								HttpServletResponse.class);			
			//调用method的方法
			me.invoke(this,request, response);
			
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

公開された108元の記事 ウォン称賛46 ビュー30000 +

おすすめ

転載: blog.csdn.net/qq_44739706/article/details/104525127