Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import javax.servlet.http.HttpSession;
  14.  
  15. /**
  16.  *
  17.  * @author valenfi1
  18.  */
  19. public class Session1 extends HttpServlet {
  20.    
  21.     /**
  22.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  23.      * @param request servlet request
  24.      * @param response servlet response
  25.      * @throws ServletException if a servlet-specific error occurs
  26.      * @throws IOException if an I/O error occurs
  27.      */
  28.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  29.     throws ServletException, IOException {
  30.         response.setContentType("text/html;charset=UTF-8");
  31.         PrintWriter out = response.getWriter();
  32.  
  33.         HttpSession ses = request.getSession(true);
  34.  
  35.         String value = null;
  36.  
  37.         if( ! ses.isNew() ) {
  38.             value = (String) ses.getAttribute("hodnota");
  39.         }
  40.  
  41.         ses.setAttribute("hodnota", request.getParameter("hodnota"));
  42.  
  43.         if( value != null ) {
  44.             out.println("<strong>původní hodnota: </strong>" + value + "<br />");
  45.         }
  46.  
  47.         out.println("<strong>nová hodnota: </strong>" + request.getParameter("hodnota") + "<br />");
  48.         out.println("<a href=\"index.html\">zpět na form</a>");
  49.  
  50.         out.close();
  51.        
  52.  
  53.     }
  54.  
  55.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  56.     /**
  57.      * Handles the HTTP <code>GET</code> method.
  58.      * @param request servlet request
  59.      * @param response servlet response
  60.      * @throws ServletException if a servlet-specific error occurs
  61.      * @throws IOException if an I/O error occurs
  62.      */
  63.     @Override
  64.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  65.     throws ServletException, IOException {
  66.         processRequest(request, response);
  67.     }
  68.  
  69.     /**
  70.      * Handles the HTTP <code>POST</code> method.
  71.      * @param request servlet request
  72.      * @param response servlet response
  73.      * @throws ServletException if a servlet-specific error occurs
  74.      * @throws IOException if an I/O error occurs
  75.      */
  76.     @Override
  77.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  78.     throws ServletException, IOException {
  79.         processRequest(request, response);
  80.     }
  81.  
  82.     /**
  83.      * Returns a short description of the servlet.
  84.      * @return a String containing servlet description
  85.      */
  86.     @Override
  87.     public String getServletInfo() {
  88.         return "Short description";
  89.     }// </editor-fold>
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement