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