📄 departmentsvlt.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBean.Department;
public class DepartmentSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String dep_id =req.getParameter("id");
int success = 0;
String action = req.getParameter("action");
Department dep = null;
String name,message="";
if ("new".equalsIgnoreCase(action)) {
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
if(isTrue(req,res,dep_id,name) && hasLogin(req,res,name)){
dep = doNew(req,res,dep_id,name);
sendBean(req, res, dep, "/getDepartment.jsp");
}
}
if ("update".equalsIgnoreCase(action)) {
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
if(isTrue(req,res,dep_id,name) && hasLogin(req,res,name)){
try{
dep = doUpdate(req,res,dep_id,name);
sendBean(req,res,dep,"/getDepartment.jsp");
}
catch(SQLException e){}
}
}
if ("delete".equalsIgnoreCase(action)) {
try{
success = doDelete(dep_id);
}
catch(SQLException e){}
if (success != 1) {
doError(req, res, "DepartmentSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/0903/getDepartment.jsp");
}
}
}
public Department doNew(HttpServletRequest req,HttpServletResponse res,String dep_id,String name)
throws ServletException,IOException{
String cname=new String (req.getParameter("cname").getBytes("ISO8859_1"));
Department dep= new Department();
dep.setId(dep_id);
dep.setName(name);
dep.setCname(cname);
dep.setClass_count(0);
dep.addDepartment();
return dep;
}
public Department doUpdate(HttpServletRequest req,HttpServletResponse res ,String id,String name)
throws ServletException,IOException,SQLException {
String cname = new String(req.getParameter("cname").getBytes("ISO8859_1"));
Department dep = new Department();
dep.setId(id);
dep.setName(name);
dep.setCname(cname);
dep.update();
return dep;
}
public int doDelete(String id) throws SQLException {
int num = 0;
Department dep=new Department();
num = dep.delete(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
Department dep, String target)
throws ServletException, IOException {
req.setAttribute("dep", dep);
RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
public void doError(HttpServletRequest req,
HttpServletResponse res,
String str)
throws ServletException, IOException {
req.setAttribute("problem", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
rd.forward(req, res);
}
public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String name)
throws ServletException, IOException{
boolean f=true;
String message="对不起,该学院已经被注册过了!";
Department dep= new Department();
f= dep.hasLogin(name);
if(f==false){
doError(req,res,message);
}
return f;
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,String dep_id,String name)
throws ServletException, IOException {
boolean f=true;
String message ="";
if(name==null || name.equals("")) {
f=false;
message="错误,院系名称不能为空!";
doError(req,res,message);
}else if(dep_id==null || dep_id.equals("")){
f=false;
message="错误,学院代号不能为空!";
doError(req,res,message);
}
return f;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -