接尾辞マッチングを使用して、従業員テーブルの削除変更を1つのサーブレットで処理


web.xmlファイルの構成は次のとおりです.
<servlet>
    <servlet-name>ManagerEmpServlet</servlet-name>
    <servlet-class>emp.ManagerEmpServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>ManagerEmpServlet</servlet-name>
    <url-pattern>*.do</url-pattern>

次はaddEmpjspファイル、コードは以下の通りです.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'addEmp.jsp' starting page</title>
  </head>
  
  <body>
   <h3>add employee</h3>
   <form action="add.do" method="post">
   name:<input type="text" name="name"><br>
   salary:<input type="text" name="salary"><br>
   <input type="submit" value="confirm">
   </form>
  </body>
</html>

データベースはexerciseで、テーブル名はempで、テーブルには3つのフィールドしか含まれていません.id int、name varchar(20)、salary double.
ManagerEmpServiceのソースコードは次のとおりです.
public class ManagerEmpServlet extends HttpServlet {

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String uri=request.getRequestURI();
		//System.out.println(uri);
		String path=uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
		Connection conn=null;
		PrintWriter pw=response.getWriter();
		if(path.equals("/list")){
			try {
				conn=DBUtil.getConnection();
				Statement stmt=conn.createStatement();
				ResultSet rs=stmt.executeQuery("select id,name,salary from emp");
				pw.println("<table border='1'>");
				pw.println("<tr><td>id</td><td>name</td><td>salary</td>" +
				"<td>operation</td></tr>");
				while(rs.next()){
					pw.println("<tr><td>"+rs.getLong("id")+"</td>"+
							"<td>"+rs.getString("name")+"</td>"
							+"<td>"+rs.getDouble("salary")+"</td>"+
							"<td><a href='delete.do?id="+rs.getLong("id")+"'>delete</a>"+
							" <a href='load.do?id="+rs.getLong("id")+"'>modify</a>" +
									"</td></tr>");
				}
				pw.println("</table>");
				pw.println("<a href='addEmp.jsp'>add employee</a>");
				pw.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.close(conn);
			}
			
		}else if(path.equals("/add")){
			String name=request.getParameter("name");
			double salary=Double.parseDouble(request.getParameter("salary"));
			try {
				conn=DBUtil.getConnection();
				PreparedStatement pstmt=conn.prepareStatement
							("insert into emp(name,salary) values(?,?)");
				pstmt.setString(1,name);
				pstmt.setDouble(2,salary);
				pstmt.executeUpdate();
				response.sendRedirect("list.do");
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.close(conn);
			}
		}else if(path.equals("/delete")){
			try {
				Long id=Long.parseLong(request.getParameter("id"));
				conn=DBUtil.getConnection();
				PreparedStatement pstmt=conn.prepareStatement("delete from emp where id=?");
				pstmt.setLong(1,id);
				pstmt.executeUpdate();
				response.sendRedirect("list.do");
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.close(conn);
			}
		}else if(path.equals("/update")){
				String id=request.getParameter("id");
				String name=request.getParameter("name");
				String salary=request.getParameter("salary");
				conn=DBUtil.getConnection();
				try {
					PreparedStatement pstmt=conn.prepareStatement(
							"update emp set name=?,salary=? where id=?");
					pstmt.setString(1,name);
					pstmt.setDouble(2,Double.parseDouble(salary));
					pstmt.setLong(3,Long.parseLong(id));
					pstmt.executeUpdate();
					response.sendRedirect("list.do");
				} catch (SQLException e) {
					e.printStackTrace();
				}finally{
					DBUtil.close(conn);
				}
			
		}else if(path.equals("/load")){
			String id=request.getParameter("id");
			conn=DBUtil.getConnection();
			try {
				PreparedStatement pstmt=conn.prepareStatement(
						"select * from emp where id=?");
				pstmt.setLong(1,Long.parseLong(id));
				ResultSet rs=pstmt.executeQuery();
				rs.next();
				pw.println("<form action='update.do?id="+id+"' method='post'>");
				pw.println("name:<input type='text' name='name' " +
					"value='"+rs.getString("name")+"'/><br>");
				pw.println("salary:<input type='text' name='salary' " +
						"value='"+rs.getDouble("salary")+"'/><br>");
				pw.println("<input type='submit' value='confirm'/>");
				pw.println("</form>");
				pw.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.close(conn);
			}
			
		}

	}

}

DBUtilはツールクラスで、getConnection()とclose(Connection conn)の2つのメソッドが含まれています.比較的簡単ですが、ここでは貼り付けません.
配置が完了したら、ブラウザのアドレスバーに次のように入力します.http://localhost:8080/web02_demo/list.doあ、車に戻ればいいです.