📄 servletask.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 Servletask extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int id = 0;
int success = 0;
HttpSession session = req.getSession(true);
String sort = null;
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) {
}
AskBean asks = null;
if (sort != null) {
if ("new".equalsIgnoreCase(action)) {
asks = doNew(req, res);
}
if ("update".equalsIgnoreCase(action)) {
try {
asks = doUpdate(req, res, id);
sendBean(req, res, asks, "answer.jsp?ask_id=" + id);
} catch (SQLException e) {
}
}
if ("delete".equalsIgnoreCase(action)) {
if (sort.equals("admin")) {
try {
success = doDelete(id);
} catch (SQLException e) {
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
if (success != 1) {
doError(req, res, "删除失败. Rows affected: " + success);
} else {
res.sendRedirect("index.jsp");
}
}
} else
doError(req, res, "你无权操作,或你还没有登陆!");
}
public AskBean doNew(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
AskBean asks = new AskBean();
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"));
String sort = new String(req.getParameter("sort").getBytes("ISO8859_1"));
int grade_id = 0, class_id = 0, u_id = 0, answer_id = 0, subject_id = 0;
try {
u_id = Integer.valueOf(req.getParameter("u_id")).intValue();
answer_id = Integer.valueOf(req.getParameter("answer_id"))
.intValue();
subject_id = Integer.valueOf(req.getParameter("subject_id"))
.intValue();
class_id = Integer.valueOf(req.getParameter("class_id")).intValue();
grade_id = Integer.valueOf(req.getParameter("grade_id")).intValue();
} catch (NumberFormatException e) {
}
try {
asks.setSort(sort);
asks.setTitle(title);
asks.setContent(content);
asks.setTime(time);
asks.setU_id(u_id);
asks.setAnswer_id(answer_id);
asks.setClass_id(class_id);
asks.setGrade_id(grade_id);
asks.setSubject_id(subject_id);
asks.addAsk();
res.sendRedirect("asklist.jsp?grade_id=" + grade_id + "&class_id="
+ class_id + "&subject_id=" + subject_id);
} catch (NullPointerException e) {
}
return asks;
}
public AskBean doUpdate(HttpServletRequest req, HttpServletResponse res,
int id) throws ServletException, IOException, SQLException {
AskBean asks = new AskBean();
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"));
// int u_id=0;
// try{
// u_id=Integer.valueOf(req.getParameter("u_id")).intValue();
//
// }catch(NumberFormatException e){}
if (isTrue(req, res, title, content)) {
asks.setTitle(title);
asks.setContent(content);
asks.setTime(time);
// asks.setU_id(u_id);
asks.updateAsk(id);
}
return asks;
}
public int doDelete(int id) throws SQLException {
int num = 0;
AskBean asks = new AskBean();
num = asks.deleteAsk(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
AskBean asks, String target) throws ServletException, IOException {
req.setAttribute("asks", asks);
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.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 + -