Revision 11
| LogServlet.java | ||
|---|---|---|
| 1 |
/* |
|
| 2 |
* To change this template, choose Tools | Templates |
|
| 3 |
* and open the template in the editor. |
|
| 4 |
*/ |
|
| 5 |
|
|
| 6 | 1 |
package org.lidar; |
| 7 | 2 |
|
| 8 | 3 |
import com.google.gson.Gson; |
| ... | ... | |
| 19 | 14 |
import org.lidar.HibernateUtil; |
| 20 | 15 |
|
| 21 | 16 |
/** |
| 22 |
* |
|
| 17 |
* Log Servlet
|
|
| 23 | 18 |
* @author Andrej Cimpersek |
| 24 | 19 |
*/ |
| 25 | 20 |
public class LogServlet extends HttpServlet {
|
| 26 |
|
|
| 27 |
/**
|
|
| 21 |
|
|
| 22 |
/** |
|
| 28 | 23 |
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. |
| 29 | 24 |
* @param request servlet request |
| 30 | 25 |
* @param response servlet response |
| ... | ... | |
| 32 | 27 |
* @throws IOException if an I/O error occurs |
| 33 | 28 |
*/ |
| 34 | 29 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
| 35 |
throws ServletException, IOException {
|
|
| 30 |
throws ServletException, IOException {
|
|
| 36 | 31 |
response.setContentType("application/json;charset=UTF-8");
|
| 37 | 32 |
PrintWriter out = response.getWriter(); |
| 38 | 33 |
Gson gson = new Gson(); |
| 39 | 34 |
try {
|
| 40 | 35 |
int start = 0; |
| 41 | 36 |
int limit = 15; |
| 42 |
if(request.getParameter("limit") != null){
|
|
| 37 |
if (request.getParameter("limit") != null) {
|
|
| 43 | 38 |
limit = Integer.parseInt(request.getParameter("limit"));
|
| 44 | 39 |
} |
| 45 |
if(request.getParameter("start") != null){
|
|
| 40 |
if (request.getParameter("start") != null) {
|
|
| 46 | 41 |
start = Integer.parseInt(request.getParameter("start"));
|
| 47 | 42 |
} |
| 48 | 43 |
|
| ... | ... | |
| 54 | 49 |
Query query = session.createQuery("from Log as log order by log.date desc");
|
| 55 | 50 |
query.setFirstResult(start); |
| 56 | 51 |
query.setMaxResults(limit); |
| 57 |
JSONResponse resp = new JSONResponse(true, total, (ArrayList)query.list()); |
|
| 52 |
JSONResponse resp = new JSONResponse(true, total, (ArrayList) query.list());
|
|
| 58 | 53 |
out.write(gson.toJson(resp)); |
| 59 | 54 |
tx.commit(); |
| 60 | 55 |
} catch (Exception e) {
|
| ... | ... | |
| 66 | 61 |
} |
| 67 | 62 |
|
| 68 | 63 |
class JSONResponse {
|
| 64 |
|
|
| 69 | 65 |
private boolean success = true; |
| 70 | 66 |
private int total; |
| 71 | 67 |
private ArrayList rows; |
| 72 | 68 |
|
| 73 |
public JSONResponse(boolean success, int total, ArrayList rows){
|
|
| 69 |
public JSONResponse(boolean success, int total, ArrayList rows) {
|
|
| 74 | 70 |
this.success = success; |
| 75 | 71 |
this.total = total; |
| 76 | 72 |
this.rows = rows; |
| ... | ... | |
| 78 | 74 |
} |
| 79 | 75 |
|
| 80 | 76 |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
| 81 |
/**
|
|
| 77 |
/** |
|
| 82 | 78 |
* Handles the HTTP <code>GET</code> method. |
| 83 | 79 |
* @param request servlet request |
| 84 | 80 |
* @param response servlet response |
| ... | ... | |
| 87 | 83 |
*/ |
| 88 | 84 |
@Override |
| 89 | 85 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 90 |
throws ServletException, IOException {
|
|
| 86 |
throws ServletException, IOException {
|
|
| 91 | 87 |
processRequest(request, response); |
| 92 |
}
|
|
| 88 |
} |
|
| 93 | 89 |
|
| 94 |
/**
|
|
| 90 |
/** |
|
| 95 | 91 |
* Handles the HTTP <code>POST</code> method. |
| 96 | 92 |
* @param request servlet request |
| 97 | 93 |
* @param response servlet response |
| ... | ... | |
| 100 | 96 |
*/ |
| 101 | 97 |
@Override |
| 102 | 98 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 103 |
throws ServletException, IOException {
|
|
| 99 |
throws ServletException, IOException {
|
|
| 104 | 100 |
processRequest(request, response); |
| 105 | 101 |
} |
| 106 | 102 |
|
| 107 |
/**
|
|
| 103 |
/** |
|
| 108 | 104 |
* Returns a short description of the servlet. |
| 109 | 105 |
* @return a String containing servlet description |
| 110 | 106 |
*/ |
| ... | ... | |
| 112 | 108 |
public String getServletInfo() {
|
| 113 | 109 |
return "Short description"; |
| 114 | 110 |
}// </editor-fold> |
| 115 |
|
|
| 116 | 111 |
} |
Also available in: Unified diff