1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-vyuka/src/java/cz/frantovo/sql/vyuka/ajax/Servlet.java Thu May 28 23:12:19 2009 +0200
1.3 @@ -0,0 +1,120 @@
1.4 +package cz.frantovo.sql.vyuka.ajax;
1.5 +
1.6 +import java.io.IOException;
1.7 +import java.io.PrintWriter;
1.8 +import javax.servlet.ServletException;
1.9 +import javax.servlet.http.HttpServlet;
1.10 +import javax.servlet.http.HttpServletRequest;
1.11 +import javax.servlet.http.HttpServletResponse;
1.12 +
1.13 +/**
1.14 + * Servlet pro vyřizování AJAXových požadavků.
1.15 + * @author fiki
1.16 + */
1.17 +public class Servlet extends HttpServlet {
1.18 +
1.19 + private static final long serialVersionUID = 9102108273105288056L;
1.20 +
1.21 + private enum akce {
1.22 +
1.23 + /** Provede SQL dotaz. */
1.24 + vykonat,
1.25 + /** Vypíše historii SQL příkazů daného uživatele. */
1.26 + historie,
1.27 + /** Vypíše nějaké nepotřebné informace. */
1.28 + test,
1.29 + /** Pokud je požadovaná akce nesmysl. */
1.30 + chyba
1.31 + }
1.32 +
1.33 + /**
1.34 + * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
1.35 + * @param request servlet request
1.36 + * @param response servlet response
1.37 + * @throws ServletException if a servlet-specific error occurs
1.38 + * @throws IOException if an I/O error occurs
1.39 + */
1.40 + protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1.41 +
1.42 + /** Používáme pouze UTF-8 */
1.43 + response.setContentType("text/html;charset=UTF-8");
1.44 + request.setCharacterEncoding("UTF-8");
1.45 +
1.46 + PrintWriter out = response.getWriter();
1.47 +
1.48 + try {
1.49 +
1.50 + akce parametrAkce = akce.chyba;
1.51 + try {
1.52 + parametrAkce = akce.valueOf(request.getParameter("akce"));
1.53 + } catch (Exception e) {
1.54 + /** Chyba nebo podvržený AJAXový požadavek → zobrazíme chybovou hlášku */
1.55 + }
1.56 +
1.57 + Ajax a = new Ajax();
1.58 +
1.59 +
1.60 + switch (parametrAkce) {
1.61 + case vykonat:
1.62 + out.println(a.geSQLVysledek(request.getParameter("sql")));
1.63 + break;
1.64 + case historie:
1.65 + out.println(a.geSQLVysledek(request.getRequestedSessionId()));
1.66 + break;
1.67 + case test:
1.68 + out.println("<p>AJAX jede!</p>");
1.69 + out.println("<ol>");
1.70 + out.println("<li>Metoda: " + request.getMethod() + "</li>");
1.71 + out.println("<li>Host: " + request.getRemoteHost() + "</li>");
1.72 + out.println("<li>Adresa: " + request.getRemoteAddr() + "</li>");
1.73 + out.println("<li>Akce: " + request.getParameter("akce") + "</li>");
1.74 + out.println("<li>SQL: " + request.getParameter("sql") + "</li>");
1.75 + out.println("<li>Sezení: " + request.getRequestedSessionId() + "</li>");
1.76 + out.println("<li>URL: " + request.getRequestURL().toString() + "</li>");
1.77 + out.println("</ol>");
1.78 + break;
1.79 + case chyba:
1.80 + out.println("<p>chyba</p>");
1.81 + break;
1.82 + }
1.83 + } finally {
1.84 + out.close();
1.85 + }
1.86 + }
1.87 +
1.88 + // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
1.89 + /**
1.90 + * Handles the HTTP <code>GET</code> method.
1.91 + * @param request servlet request
1.92 + * @param response servlet response
1.93 + * @throws ServletException if a servlet-specific error occurs
1.94 + * @throws IOException if an I/O error occurs
1.95 + */
1.96 + @Override
1.97 + protected void doGet(HttpServletRequest request, HttpServletResponse response)
1.98 + throws ServletException, IOException {
1.99 + processRequest(request, response);
1.100 + }
1.101 +
1.102 + /**
1.103 + * Handles the HTTP <code>POST</code> method.
1.104 + * @param request servlet request
1.105 + * @param response servlet response
1.106 + * @throws ServletException if a servlet-specific error occurs
1.107 + * @throws IOException if an I/O error occurs
1.108 + */
1.109 + @Override
1.110 + protected void doPost(HttpServletRequest request, HttpServletResponse response)
1.111 + throws ServletException, IOException {
1.112 + processRequest(request, response);
1.113 + }
1.114 +
1.115 + /**
1.116 + * Returns a short description of the servlet.
1.117 + * @return a String containing servlet description
1.118 + */
1.119 + @Override
1.120 + public String getServletInfo() {
1.121 + return "Servlet pro zpracování AJAXových požadavků.";
1.122 + }// </editor-fold>
1.123 +}