Advertisement
MagnusArias

PS2 | Servlets - Cookies

Apr 3rd, 2017
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.Cookie;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15.  
  16. /**
  17.  *
  18.  * @author student_10
  19.  */
  20. public class NewServlet extends HttpServlet {
  21.  
  22.    
  23.     /**
  24.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  25.      * methods.
  26.      *
  27.      * @param request servlet request
  28.      * @param response servlet response
  29.      * @throws ServletException if a servlet-specific error occurs
  30.      * @throws IOException if an I/O error occurs
  31.      */
  32.    
  33.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  34.             throws ServletException, IOException {
  35.                
  36.         response.setContentType("text/html;charset=UTF-8");
  37.         PrintWriter out = response.getWriter();
  38.        
  39.         Cookie[] cookies= request.getCookies();
  40.         Cookie licznik= null;
  41.        
  42.         try {
  43.            
  44.             if (cookies != null)
  45.             {
  46.                 for (int i = 0; i < cookies.length; i++)
  47.                 {
  48.                     if (cookies[i].getName().equals("licznik"))
  49.                     {
  50.                         licznik = cookies[i];
  51.                         break;
  52.                     }
  53.                 }
  54.             }
  55.             if (licznik == null)
  56.             {
  57.                 licznik =new Cookie("licznik", "0");
  58.             }
  59.             else
  60.             {
  61.                 Integer v = Integer.parseInt(licznik.getValue());
  62.                 v++;
  63.                 licznik.setValue(v+"");
  64.             }
  65.            
  66.             licznik.setMaxAge(1); // wartosc w sekundach, -1 to nieskonczonosc
  67.             response.addCookie(licznik);
  68.            
  69.             out.println(licznik.getValue());
  70.                
  71.         }
  72.         finally {
  73.             out.close();
  74.         }
  75.     }
  76.  
  77.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  78.     /**
  79.      * Handles the HTTP <code>GET</code> method.
  80.      *
  81.      * @param request servlet request
  82.      * @param response servlet response
  83.      * @throws ServletException if a servlet-specific error occurs
  84.      * @throws IOException if an I/O error occurs
  85.      */
  86.     @Override
  87.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  88.             throws ServletException, IOException {
  89.         processRequest(request, response);
  90.        
  91.        
  92.     }
  93.  
  94.     /**
  95.      * Handles the HTTP <code>POST</code> method.
  96.      *
  97.      * @param request servlet request
  98.      * @param response servlet response
  99.      * @throws ServletException if a servlet-specific error occurs
  100.      * @throws IOException if an I/O error occurs
  101.      */
  102.     @Override
  103.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  104.             throws ServletException, IOException {
  105.         processRequest(request, response);
  106.     }
  107.  
  108.     /**
  109.      * Returns a short description of the servlet.
  110.      *
  111.      * @return a String containing servlet description
  112.      */
  113.     @Override
  114.     public String getServletInfo() {
  115.         return "Short description";
  116.     }// </editor-fold>
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement