ITeye博客

如何让一个Servlet类处理多个业务请求

alpha2009 · 2011-06-16 16:24 · し☆JavaWeb☆ · Web前端 · Markdown

1.创建一个Servlet,将其访问设置为*.do

    <url-pattern>*.do</url-pattern>

2.一个请求url格式:http://localhost:8080/*.do(*代表自定义请求名)

3. Servlet获取请求路径名,根据路径名称执行相应的业务方法

 public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

      String uri = request.getRequestURI();
      String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
      response.setContentType("text/html;charset=utf-8");
      PrintWriter out = response.getWriter();
      /**根据用户提交的请求与对应的业务逻辑进行匹配判断*/

      if(path.equals("/请求名")){
          //do.....
      }else if(path.equals("/请求名")){
         //do.....
      }

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。