文件太大 上传不了。。。。我给你看下部分代码
public class CommentServlet extends HttpServlet {
private static final long serialVersionUID = -2540110714710583202L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取从放入request中的method的值
request.setCharacterEncoding("UTF-8");
String method = request.getParameter("method");
if (method == null) {
method = "";
}
//判断method的值调用不同方法
if (method.equals("add")) {
add(request, response);
} else if (method.equals("admin")) {
admin(request, response);
} else if (method.equals("delete")) {
delete(request, response);
} else {
admin(request, response);
}
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String content = request.getParameter("content");
String blog_id = request.getParameter("blog_id");
if (name == null | name.equals("")) {
name = "匿名";
}
String sql = "insert into comment (username,content,blog_id,createdtime) values(?,?,?,now())";
String params[] = { name, content, blog_id };
QueryRunner qr = DbHelper.getQueryRunner();
try {
qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("/blog/servlet/HomeServlet?method=display&id="
+ blog_id);
}
public void admin(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
response.sendRedirect("/blog");
} else {
String sql = "select c.id,username,c.content,c.createdtime,title from comment c,blog b where blog_id=b.id order by c.id desc";
QueryRunner qr = DbHelper.getQueryRunner();
List list = null;
try {
list = (List) qr.query(sql, new BeanListHandler(Comment.class));
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("list", list);
request.getRequestDispatcher("/admin/adminCommentList.jsp")
.forward(request, response);
}
}
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
response.sendRedirect("/blog");
} else {
String id = request.getParameter("id");
String ctid = request.getParameter("ctid");
String id2 = (String) request.getParameter("id2");
System.out.println(id2);
if (ctid != null) {
id = ctid;
}
String sql = "delete from comment where id=" + id;
QueryRunner qr = DbHelper.getQueryRunner();
try {
qr.update(sql);
} catch (SQLException e) {
e.printStackTrace();
}
if (ctid == null) {
response.sendRedirect("CommentServlet?method=admin");
} else {
request.setAttribute("id2", id2);
request.getRequestDispatcher("HomeServlet?method=display")
.forward(request, response);
}
}
}
}
下面这个类是连接数据库的
package cn.com.jobedu.blog;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
public class DbHelper {
public static QueryRunner getQueryRunner() {
// 数据源对象可以理解为连接池的管理者,通过他可以获取数据库的链接
DataSource ds = null;
Context context;
try {
// 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
context = new InitialContext();
ds = (DataSource) context.lookup("java:comp/env/jdbc/mysqlds");
} catch (NamingException e) {
System.out.println("获取数据源失败!");
}
QueryRunner qr = new QueryRunner(ds);
return qr;
}
}