?? 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="對不起,該教師號已經(jīng)被注冊過了!";
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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -