一个servlet处理多个请求

来源:互联网 发布:jsp java注释 编辑:程序博客网 时间:2024/06/05 00:43

                                                          两种


一。通过表单或者url传参command,servlet内用switch(command)来选择相应的处理方法


二。通过配置web.xml,得到servletPath,得到方法名,通过反射执行对应方法

第一步: web.xml的配置

   *.do


第二步:作为controler的servlet的书写




   String servletName = request.getServletPath();//   /******.do               String command = servletName.substring(1, servletName.length() - 3);//去除.do和“/"字符              try {            Method method = this.getClass().getDeclaredMethod(command, HttpServletRequest.class, HttpServletResponse.class);            method.invoke(this, request, response);        } catch (NoSuchMethodException ex) {        } catch (SecurityException ex) {        } catch (IllegalAccessException ex) {        } catch (IllegalArgumentException ex) {        } catch (InvocationTargetException ex) {        }


第三步:使用

例如<form action="login.do" method="post">

然后我们在controler 的servlet中添加private void login(){//balabala};相应方法即可





0 0
原创粉丝点击