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

本文展示了如何在Java Servlet中实现学生管理的CRUD操作。通过StudentServlet类,详细讲解了添加、列出、更新和删除学生信息的方法调用过程,涉及DAO层与视图跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是我写的一个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 {
 
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值