📄 servletuteacher.java
字号:
package edu;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Servletuteacher extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int id = 0;
HttpSession session = req.getSession(true);
int success = 0;
String action = null;
action = req.getParameter("action");
try {
id = Integer.valueOf(req.getParameter("id")).intValue();
} catch (NumberFormatException e) {
}
UteacherBean teacher = null;
// String message = "";
String sort = null;
try {
sort = session.getAttribute("sort").toString();
} catch (NullPointerException e) {
}
if (sort.equals("admin")) {
if ("new".equalsIgnoreCase(action)) {
teacher = doNew(req, res);
sendBean(req, res, teacher, "/ok_ad.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try {
teacher = doUpdate(req, res, id);
sendBean(req, res, teacher, "/getuteacher.jsp");
} catch (SQLException e) {
}
}
if ("delete".equalsIgnoreCase(action)) {
try {
success = doDelete(id);
} catch (SQLException e) {
}
if (success != 1) {
doError(req, res, "删除失败. Rows affected: " + success);
} else {
res.sendRedirect("getuteacher.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public UteacherBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
UteacherBean teacher = new UteacherBean();
int t_id = 0;
try {
t_id = Integer.valueOf(req.getParameter("t_id")).intValue();
} catch (NumberFormatException e) {
}
String about = new String(req.getParameter("about").getBytes(
"ISO8859_1"));
if (isTrue(req, res, about) && !hasLogin(req, res, t_id)) {
teacher.setT_id(t_id);
teacher.setAbout(about);
teacher.addTeacher();
}
return teacher;
}
public UteacherBean doUpdate(HttpServletRequest req,
HttpServletResponse res, int id) throws ServletException,
IOException, SQLException {
UteacherBean teacher = new UteacherBean();
String about = new String(req.getParameter("about").getBytes(
"ISO8859_1"));
if (isTrue(req, res, about)) {
teacher.setId(id);
teacher.setAbout(about);
teacher.updateTeacher();
}
return teacher;
}
public int doDelete(int id) throws SQLException {
int num = 0;
UteacherBean teacher = new UteacherBean();
num = teacher.deleteTeacher(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
UteacherBean teacher, String target) throws ServletException,
IOException {
req.setAttribute("teacher", teacher);
try {
RequestDispatcher rd = req.getRequestDispatcher(target);
rd.forward(req, res);
} catch (Throwable t) {
}
}
public void doError(HttpServletRequest req, HttpServletResponse res,
String str) throws ServletException, IOException {
req.setAttribute("problem", str);
try {
RequestDispatcher rd = req
.getRequestDispatcher("/errorpage_ad.jsp");
rd.forward(req, res);
} catch (Throwable t) {
}
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String about) throws ServletException, IOException {
boolean f = true;
String message = "";
if (about == null || about.equals("")) {
f = false;
message = "简介不能为空,请重新填写!";
doError(req, res, message);
}
return f;
}
public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,
int id) throws ServletException, IOException {
boolean f = true;
String message = "对不起,该帐号已经是优秀教师了,请不要重复设置!";
TeacherBean teachers = new TeacherBean();
f = teachers.uhasLogin(id);
if (f == true) {
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 + -