📄 teachersvlt.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBean.Teacher;
public class TeacherSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String tea_id =req.getParameter("id");
int success = 0;
String action = req.getParameter("action");
Teacher tea = null;
String name,password,message="";
if ("new".equalsIgnoreCase(action)){
tea_id=req.getParameter("id");
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
password= req.getParameter("password");
if(isTrue(req,res,tea_id,name,password) && hasLogin(req,res,tea_id)){
tea = doNew(req,res,tea_id,name,password);
sendBean(req, res, tea, "/getTeacher.jsp");
}
}
if ("update".equalsIgnoreCase(action)) {
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
password = req.getParameter("password");
if(isTrue(req,res,tea_id,name,password)){
try{
tea = doUpdate(req,res,tea_id,name,password);
sendBean(req,res,tea,"/getTeacher.jsp");
}
catch(SQLException e){}
}
}
if ("delete".equalsIgnoreCase(action)) {
try{
success = doDelete(tea_id);
}
catch(SQLException e){}
if (success != 1) {
doError(req, res, "TeacherSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/0903/getTeacher.jsp");
}
}
}
public Teacher doNew(HttpServletRequest req,HttpServletResponse res,String tea_id,String name,String password)
throws ServletException,IOException{
String title=new String (req.getParameter("title").getBytes("ISO8859_1"));
String dep = req.getParameter("dep");
String entryyear = req.getParameter("entryyear");
Teacher tea= new Teacher();
tea.setId(tea_id);
tea.setName(name);
tea.setPassword(password);
tea.setTitle(title);
tea.setDepart(dep);
tea.setEntryyear(entryyear);
tea.addTeacher();
return tea;
}
public Teacher doUpdate(HttpServletRequest req,HttpServletResponse res ,String id,String name,String password)
throws ServletException,IOException,SQLException {
String title = new String(req.getParameter("title").getBytes("ISO8859_1"));
String dep = req.getParameter("dep");
String entryyear = req.getParameter("entryyear");
Teacher tea = new Teacher();
tea.setId(id);
tea.setName(name);
tea.setPassword(password);
tea.setTitle(title);
tea.setDepart(dep);
tea.setEntryyear(entryyear);
tea.update();
return tea;
}
public int doDelete(String id) throws SQLException {
int num=0;
Teacher tea=new Teacher();
num=tea.delete(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
Teacher tea, String target)
throws ServletException, IOException {
req.setAttribute("tea", tea);
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 id)
throws ServletException, IOException{
boolean f=true;
String message="对不起,该教师号已经被注册过了!";
Teacher tea= new Teacher();
f= tea.hasLogin(id);
if(f==false){
doError(req,res,message);
}
return f;
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id,String name,String password)
throws ServletException, IOException {
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="错误,教师号不能为空!";
doError(req,res,message); }
else if(name==null || name.equals("")) {
f=false;
message="教师姓名不能为空,请重新填写!";
doError(req,res,message); }
else if(password==null || password.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 + -