Java Servlet 中的许多方法调用示例

时间:2020-12-13 21:05:17

以下是我写的一个Student的Servlet类,其中包含增删改查 ,在Servlet中的方法调用

package com.stu.servlet;

import java.io.IOException;


import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.stu.bean.Student;
import com.stu.dao.ClasssDAO;
import com.stu.dao.StudentDAO;


public class StudentServlet extends HttpServlet {

public StudentServlet() {
  super();
}


public void destroy() {
  super.destroy();
}

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

  ClasssDAO studao = new ClasssDAO();
  List list = studao.classList();
  request.setAttribute("classslist", list);
  request.getRequestDispatcher("student/add.jsp").forward(request, response);
}
public void list(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException, SQLException {

  StudentDAO studao = new StudentDAO();
  List list = studao.studentList();
  request.setAttribute("list", list);
  request.getRequestDispatcher("student/list.jsp").forward(request,response);
}

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

         String name = request.getParameter("name");
         String sex = request.getParameter("sex");
         String phone = request.getParameter("phone");
         int classsId = Integer.parseInt(request.getParameter("classsId"));
        
         StudentDAO studao = new StudentDAO();
         int row = studao.add(name, sex, phone, classsId);
        
         request.getSession().setAttribute("row", row);
         response.sendRedirect("result.jsp");

}


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

   int id = Integer.parseInt(request.getParameter("id"));
  
   StudentDAO studao = new StudentDAO();
   Student stu = studao.getstudentById(id);
  
   List list = new ClasssDAO().classList();
  
   request.setAttribute("stu", stu);
   request.setAttribute("classsList", list);
   request.getRequestDispatcher("student/update.jsp").forward(request, response);
}

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

   int id = Integer.parseInt(request.getParameter("id"));
   String name = request.getParameter("name");
   String sex = request.getParameter("sex");
   String phone = request.getParameter("phone");
   int classsId = Integer.parseInt(request.getParameter("classsId"));
  
   StudentDAO studao = new StudentDAO();
   int row = studao.update(id,name,sex,phone,classsId);
  
   request.getSession().setAttribute("row",row);
   response.sendRedirect("result.jsp");


}


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

   int id = Integer.parseInt(request.getParameter("id"));
  
   StudentDAO stu = new StudentDAO();
   int row = stu.delete(id);
  
   request.getSession().setAttribute("row", row);
   response.sendRedirect("result.jsp");
}


public void process(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException, SQLException {
 
 
   String method = request.getParameter("method");
   if (null == method ||"list".equals(method)) {
        list(request, response);
        return;
   }
   else if ("add".equals(method)) {
    add(request, response);
    return;
   }else if("addProcess".equals(method)){
    addProcess(request, response);
    return;
   }else if("update".equals(method)){
    update(request, response);
    return;
   }else if("updateProcess".equals(method)){
    updateProcess(request, response);
    return;
   }else if("delete".equals(method)){
    delete(request, response);
    return;
   }
  

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  try {
   process(request, response);
 
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  try {
   process(request, response);
 
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }

public void init() throws ServletException {
 
}
}