---
title: "如何让一个Servlet类处理多个业务请求"
author: "alpha2009"
date: "2011-06-16T16:24:01Z"
tags: []
categories: ["Web前端"]
slug: "servlet"
draft: false
lastmod: "2011-06-16T18:08:36Z"
id: 1095125
url: "https://alpha2009.iteye.com/blog/1095125"
---

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

    &lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;

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.....  
      }

}