-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetCommentServlet.java
More file actions
91 lines (83 loc) · 2.85 KB
/
GetCommentServlet.java
File metadata and controls
91 lines (83 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import bean.Comment;
import bean.User;
import dao.CommentDao;
import dao.UserDao;
//»ñÈ¡ÆÀÂÛ
@SuppressWarnings("serial")
public class GetCommentServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@SuppressWarnings("unused")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/plain;charset=utf-8");
PrintWriter out=resp.getWriter();
String strStart=req.getParameter("start");
String strEnd=req.getParameter("end");
String strPid=req.getParameter("pid");
String hot=req.getParameter("hot");
List<String[]> agree=new ArrayList<String[]>();
if(strStart==null||strEnd==null||strPid==null)return;
int start=Integer.parseInt(strStart);
int end=Integer.parseInt(strEnd);
int pid=Integer.parseInt(strPid);
if(hot!=null){
CommentDao cd=new CommentDao();
List<Comment> coms=cd.getHotComment(pid); //»ñÈ¡ÈÈÃÅÆÀÂÛ
if(coms.size()==0){
out.print("[]");
return;
}
JSONArray jsonArray=new JSONArray();
UserDao ud=new UserDao();
for(Comment c:coms){
User u=ud.getUser(c.getUser_id());
JSONObject json=new JSONObject();
json.put("cid", c.getComment_id());
json.put("uid", u.getUser_id());
json.put("nickname", u.getNickname());
json.put("uicon", u.getUser_icon());
json.put("time", c.getTime());
json.put("agree", c.getAgree());
json.put("content", c.getContent());
jsonArray.put(json);
}
out.print(jsonArray.toString());
return;
}
CommentDao cd=new CommentDao();
List<Comment> coms=cd.getRangeOfCommentByDesc(start, end, pid);
if(coms.size()==0){
out.print("[]");
return;
}
JSONArray jsonArray=new JSONArray();
UserDao ud=new UserDao();
for(Comment c:coms){
User u=ud.getUser(c.getUser_id());
JSONObject json=new JSONObject();
json.put("cid", c.getComment_id());
json.put("uid", u.getUser_id());
json.put("nickname", u.getNickname());
json.put("uicon", u.getUser_icon());
json.put("time", c.getTime());
json.put("agree", c.getAgree());
json.put("content", c.getContent());
jsonArray.put(json);
}
out.print(jsonArray.toString());
}
}