一个servlet中可以有多个处理请求的方法
Posted 骑着猪猛跑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个servlet中可以有多个处理请求的方法相关的知识,希望对你有一定的参考价值。
当我们访问Servlet时,发生了那些操作?
首先是通过<url-pattern>找到<servlet-name>,通过<serlvet-name>最终找到<servlet-class>,也就是类名,在通过反射得到Serlvet对象。
再由tomcat调用init()、service()、destroy()方法,这一过程是Servlet的生命周期。在HttpServlet里有个service()的重载方法。ServletRequest、ServletResponse经过service(ServletRequest req,ServletResponse resp)方法转换成Http格式的。再在service(HttpServletRequest req,HttpServletResponse resp)中调用doGet()或者doPost()方法。
路径:http://localhost:8080/baseservlet/base?method=addUser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class BaseServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getParameter( "method" ); //获取要调用的方法,其中的value="method"是自己约定的 if ( "addUser" .equals(method)){ addUser(req,resp); } if ( "deleteUser" .equals(method)){ deleteUser(); } } private void deleteUser() { System.out.println( "deleteUser()" ); } private void addUser(HttpServletRequest req, HttpServletResponse resp) { System.out.println( "add()" ); } } |
改进版
很显然,上面的代码不是我们想要的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class BaseServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter( "method" ); //获取方法名 if (name == null || name.isEmpty()){ throw new RuntimeException( "没有传递method参数,请给出你想调用的方法" ); } Class c = this .getClass(); //获得当前类的Class对象 Method method = null ; try { //获得Method对象 method = c.getMethod(name, HttpServletRequest. class ,HttpServletResponse. class ); } catch (Exception e) { throw new RuntimeException( "没有找到" +name+ "方法,请检查该方法是否存在" ); } try { method.invoke( this , req,resp); //反射调用方法 } catch (Exception e) { System.out.println( "你调用的方法" +name+ ",内部发生了异常" ); throw new RuntimeException(e); } } } |
在项目中,用一个Servlet继承该BaseServlet就可以实现多个请求处理。
自己写的
public class Selectservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Selectservlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName=request.getParameter("method");
Class<? extends Selectservlet> cla=this.getClass();
try {
Method method = cla.getDeclaredMethod(methodName,HttpServletRequest.class, HttpServletResponse.class);
method.setAccessible(true);
method.invoke(this, request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
protected void chaxun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Studentservice s=new Studentservice();
System.out.println(s);
request.setAttribute("stu", s.getStudentList());
request.getRequestDispatcher("/WEB-INF/List.jsp").forward(request, response);
}
protected void tianjia(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userid=request.getParameter("userid");
String username=request.getParameter("username");
String userschool=request.getParameter("userschool");
String userscore=request.getParameter("userscore");
Studentservice s=new Studentservice();
s.tianjia(userid, username, userschool, userscore);
Studentservice s1=new Studentservice();
request.setAttribute("stu", s1.getStudentList());
request.getRequestDispatcher("/WEB-INF/List.jsp").forward(request, response);
}
}
以上是关于一个servlet中可以有多个处理请求的方法的主要内容,如果未能解决你的问题,请参考以下文章