躲避酒驾:关于Servlet删除操作的总结

来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 11:20:49
在Servlet的doPost()函数里面的代码如下:  String pid = request.getParameter("id");
  String fromURL = request.getParameter("fromURL");
  String[] idList = request.getParameterValues("C_item");
  
  PositionDao pdao = new PositionDao();
  
  int id = 0;
  StringBuffer message = new StringBuffer();
  
  if(idList!=null)
  {
   for(int i=0;i   {
    try{
     id = Integer.parseInt(idList[i]);
     boolean b = pdao.delPositionById(id);
     if(b)
     {
      message.append("简历 "+id+" 删除成功!
");
     }else{
      message.append("简历 "+id+" 删除失败!
");
     }
    }catch(ClassCastException e){
     e.printStackTrace();
    }
   }
  }else{
   try{
    id = Integer.parseInt(pid);
    boolean b = pdao.delPositionById(id);
    if(b)
    {
     message.append("简历 "+id+" 删除成功!
");
    }else{
     message.append("简历 "+id+" 删除失败!
");
    }
   }catch(ClassCastException e)
   {
    e.printStackTrace();        
   }
   
  }
  
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("");
  out.println("");
  out.println("  A Servlet");
  out.println(" 
");
  
  out.print(message);
  out.print("");
  
  out.println("
");
  out.println("");
  out.flush();
  out.close(); 程序执行只接收了红色字体部分的参数,而后面紫色部份代码则无论删除对象是什么类型都是相似的,尤其是如果dao采用了接口模式之后,函数都是同一的,因此可以整合到单独的类当中去,节省写代码的时间。