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