源码获取:俺的博客首页 "资源" 里下载!
项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax等等。
登录管理控制层:
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain;charset=utf-8");
req.setCharacterEncoding("utf-8");
PrintWriter out=resp.getWriter();
String username=req.getParameter("username");
String password=req.getParameter("password");
HttpSession session=req.getSession();
if(username==null||password==null){
return;
}
UserDao ud=new UserDao();
User user=ud.getUser(username);
if(user!=null){
if(user.getPassword().equals(password)){
out.print("{\"state\":0}");
session.setAttribute("user", user);
out.close();
}else{
out.print("{\"state\":2}");
out.close();
}
}else{
out.print("{\"state\":1}");
out.close();
}
}
}
用户信息管理控制层:
@SuppressWarnings("serial")
public class GetUserDetailServlet extends HttpServlet{
@SuppressWarnings("unused")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;charset=utf-8");
String type=req.getParameter("type");
String uid=req.getParameter("uid");
int user_id=0;
if(uid!=null){
user_id=Integer.parseInt(uid);
}
PrintWriter out=resp.getWriter();
if(type==null)return;
HttpSession session=req.getSession();
User user=(User)session.getAttribute("user");
if(user==null){
return;
}
boolean isSmall=false;
if(user_id==user.getUser_id())
isSmall=true;
if(type.equals("getuserpost")){
UserDao ud=new UserDao();
User u=ud.getUser(user_id);//当前浏览的用户
if(u==null)return;
PostDao pd=new PostDao();
List<Post> ps=pd.getPosts(u.getUser_id());//用户发表过的帖子
JSONArray jsonArray=new JSONArray();
/*time
* pid
* title
* isSamll*/
for(Post p:ps){
JSONObject json=new JSONObject();
json.put("time", p.getPost_time());
json.put("pid", p.getPost_id());
json.put("title", p.getTitle());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
jsonArray.put(json);
}
out.print(jsonArray.toString());
}else if(type.equals("delpost")){
String pid=req.getParameter("pid");
if(pid==null)return;
int post_id=Integer.parseInt(pid);
PostDao pd=new PostDao();
Post p=pd.getPost(post_id);
if(p==null)return;
pd.delPost(post_id);
CollectionDao cld=new CollectionDao();
cld.delCollectionByPostId(post_id);
CommentDao cd=new CommentDao();
out.print("ok");
}else if(type.equals("delcollection")){
String clid=req.getParameter("clid");
if(clid==null)return;
int cl_id=Integer.parseInt(clid);
CollectionDao cld=new CollectionDao();
cld.delCollectionById(cl_id);
out.print("ok");
return;
}else if(type.equals("getusercollection")){
if(uid==null){return;}
CollectionDao cld=new CollectionDao();
List<Collection> cls=cld.getCollectionByUserId(user_id);
if(cls.size()==0)return;
PostDao pd=new PostDao();
UserDao ud=new UserDao();
JSONArray jsonArray=new JSONArray();
for(Collection c:cls){
Post p=pd.getPost(c.getPost_id());
int uuid=pd.getUserIdByPostId(p.getPost_id());
User u=ud.getUser(uuid);
JSONObject json=new JSONObject();
/*time
* cl_id
* pid
* title
* isSamll
* uid
* unickname
* pid time tile isSmall
*/
json.put("cl_id",c.getCollection_id());
json.put("pid", p.getPost_id());
json.put("title", p.getTitle());
json.put("time", c.getTime());
json.put("uid",u.getUser_id());
json.put("unickname", u.getNickname());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
jsonArray.put(json);
}
out.print(jsonArray.toString());
}else if(type.equals("getusercomment")){
if(uid==null){return;}
CommentDao cd=new CommentDao();
List<Comment> cs=cd.getCommentByUserId(user_id);
JSONArray jsonArray=new JSONArray();
for(Comment c:cs){
PostDao pd=new PostDao();
String isExist="false";
Post p=pd.getPost(c.getPost_id()); //发表的帖子
JSONObject json=new JSONObject();
if(p!=null){
isExist="true";
UserDao ud=new UserDao();
User u=ud.getUser(p.getUser_id()); //创建人
json.put("uid", u.getUser_id());
json.put("title",p.getTitle());
json.put("unickname",u.getNickname());
json.put("pid",p.getPost_id());
}
json.put("cid", c.getComment_id());
json.put("time",c.getTime());
if(isSmall){
json.put("isSmall", "true");
}else{
json.put("isSmall", "false");
}
json.put("content", c.getContent());
json.put("isExist", isExist);
jsonArray.put(json);
}
out.print(jsonArray.toString());
/*cid time isSmall content uid title unickname pid isExist*/
}else if(type.equals("delcomment")){
String cid=req.getParameter("cid");
if(cid==null)return;
int comment_id=Integer.parseInt(cid);
CommentDao cd=new CommentDao();
cd.delCommentById(comment_id);
out.print("ok");
}
}
}
评论管理控制层:
//评论相关
@SuppressWarnings("serial")
public class CommentServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String type=req.getParameter("type");
String content=req.getParameter("content");
String pid=req.getParameter("pid");
PrintWriter out=resp.getWriter();
if(type==null||content==null||pid==null){
return;
}
HttpSession session=req.getSession();
User user=(User)session.getAttribute("user");
if(user==null){
out.print("<script type='text/javascript'>"
+ "window.location.href='/jsp_xiaoshuo_site/index.jsp'"
+ "</script>");
return;
}
int post_id=Integer.parseInt(pid);
Comment com=new Comment();
com.setContent(content);
com.setPost_id(post_id);
com.setUser_id(user.getUser_id());
Date now=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time=sdf.format(now);
com.setTime(time);
CommentDao cd=new CommentDao();
cd.insert(com);
out.print("<script type='text/javascript'>"
+ "window.location.href='/jsp_xiaoshuo_site/jsp/postdetail.jsp?pid="+pid+"'"
+ "</script>");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
源码获取:俺的博客首页 "资源" 里下载!