📄 newpostservlet.java
字号:
package com.lovo.bbs.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.PostBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.PostVo;
import com.lovo.bbs.bo.TopicBo;
import com.lovo.bbs.bo.UserInfoBo;
public class NewPostServlet extends HttpServlet {
private static final long serialVersionUID = 3473498779003225960L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@SuppressWarnings("unchecked")
protected synchronized void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 取得参数
String topicid = req.getParameter("topicid");
String forumid = req.getParameter("postforumid");
String postTitle = new String(req.getParameter("posttitle").getBytes(
"ISO-8859-1"), "utf-8");
String postAuthorName = new String(req.getParameter("postauthorname")
.getBytes("ISO-8859-1"), "utf-8");
String postContent = new String(req.getParameter("postcontent")
.getBytes("ISO-8859-1"), "utf-8");
// 构造Vo
PostVo postvo = new PostVo();
postvo.setTopicid(Integer.parseInt(topicid));
postvo.setPosttitle(postTitle);
postvo.setUsername(postAuthorName);
postvo.setContent(postContent);
// 更新回帖表
int postid = new PostBo().addOnePost(postvo);
ServletContext context = req.getSession().getServletContext();
// 更新数据库论坛表当前论坛信息
new ForumBo().addOnePost(Integer.parseInt(forumid));
// 更新内存中当前论坛信息
ArrayList<ForumVo> forumList = (ArrayList<ForumVo>) context
.getAttribute("forumList");
Iterator<ForumVo> it = forumList.iterator();
ForumVo thisForum = null;
while (it.hasNext()) {
thisForum = it.next();
if (thisForum.getForumID() == Integer.parseInt(forumid)) {
break;
}
}
thisForum.setPostNum(thisForum.getPostNum()+1);
context.setAttribute("forumList", forumList);
HashMap<Integer,ForumVo> forumMap = (HashMap<Integer,ForumVo>)context.getAttribute("forumMap");
forumMap.put(thisForum.getForumID(), thisForum);
context.setAttribute("forumMap", forumMap);
//更新网站杂项信息表
new ForumStatBo().addOnePost();
//更新内存中网站杂项信息
ForumStatVo forumInfo = (ForumStatVo)context.getAttribute("forumInfo");
forumInfo.setPostNum(forumInfo.getPostNum()+1);
context.setAttribute("forumInfo", forumInfo);
//更新主题表
new TopicBo().addOnePost(postid,Integer.parseInt(topicid));
//更新用户表
new UserInfoBo().addOnePost(postAuthorName);
// 转向至新主题页
if (postid != 0) {
resp.sendRedirect("topic.jsp?tid=" + topicid);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -