package servlet;
import java.io.IOException;
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;
/**
*
* @ClassName: BaseServlet
* @Description:TODO(通用servlet)
* @author: pan
* @date: 2019年11月30日 上午9:53:56
*
*/
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp;
String methodname=request.getParameter("method");
//反射机制
Class<?> class1=this.getClass();
try {
Method method = class1.getMethod(methodname, HttpServletRequest.class,HttpServletResponse.class);
String address=(String) method.invoke(this,request, response);
if (address!=null&&!address.trim().equals("")) {
request.getRequestDispatcher(address).forward(request, response);
}
} catch (Exception e) {
throw new RuntimeException("访问出错");
}
}
}