[11] | 1 | package cz.frantovo.sql.vyuka.ajax; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.io.PrintWriter; |
---|
| 5 | import javax.servlet.ServletException; |
---|
| 6 | import javax.servlet.http.HttpServlet; |
---|
| 7 | import javax.servlet.http.HttpServletRequest; |
---|
| 8 | import javax.servlet.http.HttpServletResponse; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Servlet pro vyřizování AJAXových požadavků. |
---|
| 12 | * @author fiki |
---|
| 13 | */ |
---|
| 14 | public class Servlet extends HttpServlet { |
---|
| 15 | |
---|
| 16 | private static final long serialVersionUID = 9102108273105288056L; |
---|
| 17 | |
---|
| 18 | private enum akce { |
---|
| 19 | |
---|
| 20 | /** Provede SQL dotaz. */ |
---|
| 21 | vykonat, |
---|
| 22 | /** Vypíše historii SQL příkazů daného uživatele. */ |
---|
| 23 | historie, |
---|
| 24 | /** Vypíše nějaké nepotřebné informace. */ |
---|
| 25 | test, |
---|
| 26 | /** Pokud je požadovaná akce nesmysl. */ |
---|
| 27 | chyba |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. |
---|
| 32 | * @param request servlet request |
---|
| 33 | * @param response servlet response |
---|
| 34 | * @throws ServletException if a servlet-specific error occurs |
---|
| 35 | * @throws IOException if an I/O error occurs |
---|
| 36 | */ |
---|
| 37 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
---|
| 38 | |
---|
| 39 | /** Používáme pouze UTF-8 */ |
---|
| 40 | response.setContentType("text/html;charset=UTF-8"); |
---|
| 41 | request.setCharacterEncoding("UTF-8"); |
---|
| 42 | |
---|
| 43 | PrintWriter out = response.getWriter(); |
---|
| 44 | |
---|
| 45 | try { |
---|
| 46 | |
---|
| 47 | akce parametrAkce = akce.chyba; |
---|
| 48 | try { |
---|
| 49 | parametrAkce = akce.valueOf(request.getParameter("akce")); |
---|
| 50 | } catch (Exception e) { |
---|
| 51 | /** Chyba nebo podvržený AJAXový požadavek → zobrazíme chybovou hlášku */ |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Ajax a = new Ajax(); |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | switch (parametrAkce) { |
---|
| 58 | case vykonat: |
---|
| 59 | out.println(a.geSQLVysledek(request.getParameter("sql"))); |
---|
| 60 | break; |
---|
| 61 | case historie: |
---|
| 62 | out.println(a.geSQLVysledek(request.getRequestedSessionId())); |
---|
| 63 | break; |
---|
| 64 | case test: |
---|
| 65 | out.println("<p>AJAX jede!</p>"); |
---|
| 66 | out.println("<ol>"); |
---|
| 67 | out.println("<li>Metoda: " + request.getMethod() + "</li>"); |
---|
| 68 | out.println("<li>Host: " + request.getRemoteHost() + "</li>"); |
---|
| 69 | out.println("<li>Adresa: " + request.getRemoteAddr() + "</li>"); |
---|
| 70 | out.println("<li>Akce: " + request.getParameter("akce") + "</li>"); |
---|
| 71 | out.println("<li>SQL: " + request.getParameter("sql") + "</li>"); |
---|
| 72 | out.println("<li>Sezení: " + request.getRequestedSessionId() + "</li>"); |
---|
| 73 | out.println("<li>URL: " + request.getRequestURL().toString() + "</li>"); |
---|
| 74 | out.println("</ol>"); |
---|
| 75 | break; |
---|
| 76 | case chyba: |
---|
| 77 | out.println("<p>chyba</p>"); |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | } finally { |
---|
| 81 | out.close(); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
---|
| 86 | /** |
---|
| 87 | * Handles the HTTP <code>GET</code> method. |
---|
| 88 | * @param request servlet request |
---|
| 89 | * @param response servlet response |
---|
| 90 | * @throws ServletException if a servlet-specific error occurs |
---|
| 91 | * @throws IOException if an I/O error occurs |
---|
| 92 | */ |
---|
| 93 | @Override |
---|
| 94 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
| 95 | throws ServletException, IOException { |
---|
| 96 | processRequest(request, response); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Handles the HTTP <code>POST</code> method. |
---|
| 101 | * @param request servlet request |
---|
| 102 | * @param response servlet response |
---|
| 103 | * @throws ServletException if a servlet-specific error occurs |
---|
| 104 | * @throws IOException if an I/O error occurs |
---|
| 105 | */ |
---|
| 106 | @Override |
---|
| 107 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
| 108 | throws ServletException, IOException { |
---|
| 109 | processRequest(request, response); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Returns a short description of the servlet. |
---|
| 114 | * @return a String containing servlet description |
---|
| 115 | */ |
---|
| 116 | @Override |
---|
| 117 | public String getServletInfo() { |
---|
| 118 | return "Servlet pro zpracování AJAXových požadavků."; |
---|
| 119 | }// </editor-fold> |
---|
| 120 | } |
---|