Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 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. package pl.polsl;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.Cookie;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16.  
  17. /**
  18. *
  19. * @author Student
  20. */
  21. public class CookieServlet extends HttpServlet {
  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. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. response.setContentType("text/html;charset=UTF-8");
  35. try (PrintWriter out = response.getWriter()) {
  36. Cookie cookie;
  37. Cookie[] cookies = request.getCookies();
  38. String name = request.getParameter("name");
  39.  
  40.  
  41. if(cookies != null)
  42. {
  43. for(Cookie c : cookies)
  44. {
  45. if(c.getName().equals(name))
  46. cookie = c;
  47. break;
  48. }
  49. }
  50. if(cookie != null)
  51. {
  52. cookie.setValue(Integer.toString(
  53. Integer.parseInt(cookie.getValue()) +1));
  54. }
  55. else{
  56. cookie = new Cookie(name,"1");
  57. }
  58. out.println("Visits: " + cookie.getValue());
  59. response.addCookie(cookie);
  60. }
  61. }
  62.  
  63. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  64. /**
  65. * Handles the HTTP <code>GET</code> method.
  66. *
  67. * @param request servlet request
  68. * @param response servlet response
  69. * @throws ServletException if a servlet-specific error occurs
  70. * @throws IOException if an I/O error occurs
  71. */
  72. @Override
  73. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  74. throws ServletException, IOException {
  75. processRequest(request, response);
  76. }
  77.  
  78. /**
  79. * Handles the HTTP <code>POST</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 doPost(HttpServletRequest request, HttpServletResponse response)
  88. throws ServletException, IOException {
  89. processRequest(request, response);
  90. }
  91.  
  92. /**
  93. * Returns a short description of the servlet.
  94. *
  95. * @return a String containing servlet description
  96. */
  97. @Override
  98. public String getServletInfo() {
  99. return "Short description";
  100. }// </editor-fold>
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement