📄 servlethelp.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;
/**
* Servlet implementation class for Servlet: Servletnote
*
*/
public class Servlethelp 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) {
}
HelpBean helps = null;
// String message = "";
String sort = null;
try {
sort = session.getAttribute("sort").toString();
} catch (NullPointerException e) {
}
if (sort.equals("admin")) {
if ("new".equalsIgnoreCase(action)) {
helps = doNew(req, res);
sendBean(req, res, helps, "/gethelp.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try {
helps = doUpdate(req, res, id);
sendBean(req, res, helps, "/gethelp.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("gethelp.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public HelpBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HelpBean helps = new HelpBean();
String title = new String(req.getParameter("title").getBytes(
"ISO8859_1"));
String content = new String(req.getParameter("content").getBytes(
"ISO8859_1"));
String time = new String(req.getParameter("time").getBytes("ISO8859_1"));
helps.setTime(time);
helps.setContent(content);
helps.setTitle(title);
helps.addHelp();
return helps;
}
public HelpBean doUpdate(HttpServletRequest req, HttpServletResponse res,
int id) throws ServletException, IOException, SQLException {
HelpBean helps = new HelpBean();
String title = new String(req.getParameter("title").getBytes(
"ISO8859_1"));
String content = new String(req.getParameter("content").getBytes(
"ISO8859_1"));
if (isTrue(req, res, title, content)) {
helps.setId(id);
helps.setTitle(title);
helps.setContent(content);
helps.updateHelp();
}
return helps;
}
public int doDelete(int id) throws SQLException {
int num = 0;
HelpBean helps = new HelpBean();
num = helps.deleteHelp(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
HelpBean helps, String target) throws ServletException, IOException {
req.setAttribute("helps", helps);
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 title, String content) throws ServletException, IOException {
boolean f = true;
String message = "";
if (title == null || title.equals("")) {
f = false;
message = "主题不能为空,请重新填写!";
doError(req, res, message);
}
if (content == null || content.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 + -