Servlet不断抽取案例
ServletDemo02
package cn.itcast.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo02 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String md=request.getParameter("method");
String path=null;
//获取到当前字节码对象(ServletDemo2.class在内存中的对象)
Class clazz = this.getClass();
try {
//获取clazz上名称为md的方法
Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
if(null!=method){
//调用找到的方法
path=(String)method.invoke(this, request,response);
}
if(null!=path){
//服务端的转发
request.getRequestDispatcher(path).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return "/test.html";
}
public String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return "/test.html";
}
public String checkStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("DDDDDD");
return null;
}
}