📄 servletgrade.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 Servletgrade 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);
String sort = null;
try {
sort = session.getAttribute("sort").toString();
} catch (NullPointerException e) {
}
int success = 0;
String action = null;
action = req.getParameter("action");
try {
id = Integer.valueOf(req.getParameter("grade_id")).intValue();
} catch (NumberFormatException e) {
}
GradeBean grades = null;
// String message = "";
if (sort.equals("admin")) {
if ("new".equalsIgnoreCase(action)) {
grades = doNew(req, res);
sendBean(req, res, grades, "/addclass.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try {
grades = doUpdate(req, res, id);
sendBean(req, res, grades, "/addclass.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("addclass.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public GradeBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
GradeBean grades = new GradeBean();
String grade = new String(req.getParameter("grade").getBytes(
"ISO8859_1"));
grades.setGrade(grade);
grades.addGrade();
return grades;
}
public GradeBean doUpdate(HttpServletRequest req, HttpServletResponse res,
int grade_id) throws ServletException, IOException, SQLException {
GradeBean grades = new GradeBean();
String grade = new String(req.getParameter("grade").getBytes(
"ISO8859_1"));
if (isTrue(req, res, grade)) {
grades.setId(grade_id);
grades.setGrade(grade);
grades.updateGrade();
}
return grades;
}
public int doDelete(int id) throws SQLException {
int num = 0;
GradeBean grades = new GradeBean();
num = grades.deleteGrade(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
GradeBean grades, String target) throws ServletException,
IOException {
req.setAttribute("grades", grades);
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 grade) throws ServletException, IOException {
boolean f = true;
String message = "";
if (grade == null || grade.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 + -