📄 addcomment.java
字号:
/**
* Copyright ©? 2006 广州乐言信息科技有限公司.
* All right reserved.
* Created at 2006-4-10
*/
package com.hiany.comment.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hiany.comment.dao.CommentDao;
import com.hiany.comment.domain.Comment;
/**
* 增加一个评论
*/
public class AddComment extends HttpServlet {
static Logger logger = Logger.getLogger("CommentDaoHbn");
private static final long serialVersionUID = -9103412791017846206L;
static CommentDao commDao;
public void init() throws ServletException {
String[] serviceResources = { "beans.xml" };
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
serviceResources);
commDao = (CommentDao) applicationContext.getBean("commentDao");
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
this.doPost(req, res);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setCharacterEncoding("GBK");
res.getWriter().println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\">" +
"<HTML><HEAD><TITLE>乐言网</TITLE>" +
"<META http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><body>");
req.setCharacterEncoding("GBK");
String action = req.getParameter("action");
if(action==null || !action.equals("delete")){
try {
String referer = req.getHeader("Referer");
long newsId = Long.parseLong(req.getParameter("newsId"));
String username = req.getParameter("username");
// String password = req.getParameter("password");
String anonymous = req.getParameter("anonymous");
String content = req.getParameter("content");
logger.debug("content:"+ content);
if(content!=null && content.length()>256){
res.getWriter().println("对不起,评论内容不能超过256个字符。");
return ;
}
Comment comm = new Comment();
if (anonymous != null && anonymous.equals("1")) {
comm.setAnonymous(true);
} else {
comm.setAnonymous(false);
}
comm.setUsername(username);
comm.setCommentTime(System.currentTimeMillis());
comm.setNewsId(newsId);
comm.setIp(req.getRemoteAddr());
comm.setComment(content);
commDao.save(comm);
res.getWriter().println("谢谢你的发言.");
res.getWriter().println("<script language=javascript>parent.frames['viewcomment'].location.reload();</script>");
if(referer!=null && referer.length()>0){
res.getWriter().println("<meta http-equiv=\"refresh\" content=\"3;url="+ referer +"\">");
}
} catch (HibernateException e) {
res.getWriter().println("出错啦,请稍后再试或通知管理员.<font color=#FFFFFF>" +e.getMessage() +"</font>");
e.printStackTrace();
} catch (Exception e) {
res.getWriter().println("出错啦,请稍后再试或通知管理员.<font color=#FFFFFF>" +e.getMessage() +"</font>");
e.printStackTrace();
}
}else if(action!=null && action.equals("delete")){
try{
long id = Long.parseLong(req.getParameter("id"));
String pass = req.getParameter("pass");
if(pass!=null && pass.equals("qinglong123")){
commDao.delete(id);
res.getWriter().println("成功删除评论" + id);
}
}catch(Exception ex){
res.getWriter().println("Exception:" + ex.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -