📄 servletuuser.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 Servletuuser 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 = action = req.getParameter("action");
try {
id = Integer.valueOf(req.getParameter("id")).intValue();
} catch (NumberFormatException e) {
}
UuserBean user = null;
String message = "";
String sort = null;
try {
sort = session.getAttribute("sort").toString();
} catch (NullPointerException e) {
}
if (sort.equals("admin")) {
if ("new".equalsIgnoreCase(action)) {
user = doNew(req, res);
sendBean(req, res, user, "/ok_ad.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try {
user = doUpdate(req, res, id);
sendBean(req, res, user, "/getuuser.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("getuuser.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public UuserBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
UuserBean user = new UuserBean();
int u_id = 0;
try {
u_id = Integer.valueOf(req.getParameter("u_id")).intValue();
} catch (NumberFormatException e) {
}
String about = new String(req.getParameter("about").getBytes(
"ISO8859_1"));
if (isTrue(req, res, about) && !hasLogin(req, res, u_id)) {
user.setU_id(u_id);
user.setAbout(about);
user.addUser();
}
return user;
}
public UuserBean doUpdate(HttpServletRequest req, HttpServletResponse res,
int id) throws ServletException, IOException, SQLException {
UuserBean user = new UuserBean();
String about = new String(req.getParameter("about").getBytes(
"ISO8859_1"));
if (isTrue(req, res, about)) {
user.setId(id);
user.setAbout(about);
user.updateUser();
}
return user;
}
public int doDelete(int id) throws SQLException {
int num = 0;
UuserBean user = new UuserBean();
num = user.deleteUser(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
UuserBean user, String target) throws ServletException, IOException {
req.setAttribute("user", user);
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 = "对不起,该帐号已经是优秀学生了,请不要重复设置!";
UserBean user = new UserBean();
f = user.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 + -