📄 servletadmin.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 Servletadmin 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 admin = req.getParameter("admin");
int success = 0;
String sort = "";
try {
sort = session.getAttribute("sort").toString();
} catch (NullPointerException e) {
}
String action = null;
action = req.getParameter("action");
try {
id = Integer.valueOf(req.getParameter("id")).intValue();
} catch (NumberFormatException e) {
}
AdminBean admins = null;
// String message="";
if (sort.equals("admin")) {
if ("new".equalsIgnoreCase(action)) {
admins = doNew(req, res);
sendBean(req, res, admins, "addadmin.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try {
admins = doUpdate(req, res, admin);
sendBean(req, res, admins, "addadmin.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("addadmin.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public AdminBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
AdminBean admins = new AdminBean();
Md5Bean md5 = new Md5Bean();
String pswss = req.getParameter("pswss");
String admin = new String(req.getParameter("admin").getBytes(
"ISO8859_1"));
String psw = req.getParameter("psw");
if (isTrue(req, res, pswss, admin, psw) && hasLogin(req, res, admin)) {
psw = md5.getMD5ofStr(psw);
admins.setAdmin(admin);
admins.setPsw(psw);
admins.addAdmin();
}
return admins;
}
public AdminBean doUpdate(HttpServletRequest req, HttpServletResponse res,
String admin) throws ServletException, IOException, SQLException {
AdminBean admins = new AdminBean();
Md5Bean md5 = new Md5Bean();
int id = 0;
admin = new String(req.getParameter("admin").getBytes("ISO8859_1"));
String pswss = req.getParameter("pswss");
String psw = req.getParameter("psw");
try {
id = Integer.valueOf(req.getParameter("id")).intValue();
} catch (NumberFormatException e) {
}
if (isTrue(req, res, pswss, admin, psw)) {
psw = md5.getMD5ofStr(psw);
admins.setAdmin(admin);
admins.setPsw(psw);
admins.setId(id);
admins.updateAdmin();
}
return admins;
}
public int doDelete(int id) throws SQLException {
int num = 0;
AdminBean admins = new AdminBean();
num = admins.deleteAdmin(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
AdminBean admins, String target) throws ServletException,
IOException {
req.setAttribute("admins", admins);
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 hasLogin(HttpServletRequest req, HttpServletResponse res,
String name) throws ServletException, IOException {
boolean f = true;
String message = "对不起,该帐号已经被注册过了!";
AdminBean admins = new AdminBean();
f = admins.hasLogin(name);
if (f == false) {
doError(req, res, message);
}
return f;
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String pswss, String name, String psw) throws ServletException,
IOException {
boolean f = true;
String message = "";
if (name == null || name.equals("")) {
f = false;
message = "帐号不能为空,请重新填写!";
doError(req, res, message);
}
if (psw == null || psw.equals("")) {
f = false;
message = "密码不能为空,请重新填写!";
doError(req, res, message);
}
if (pswss.equals("") != psw.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 + -