SHARE
TWEET

dfr

a guest Oct 21st, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package servlets;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.Cookie;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. /**
  14.  * Servlet implementation class WelcomeServlet
  15.  */
  16. @WebServlet("/WelcomeServlet")
  17. public class WelcomeServlet extends HttpServlet {
  18.     private static final long serialVersionUID = 1L;
  19.        
  20.     /**
  21.      * @see HttpServlet#HttpServlet()
  22.      */
  23.     public WelcomeServlet() {
  24.         super();
  25.         // TODO Auto-generated constructor stub
  26.     }
  27.  
  28.     /**
  29.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  30.      */
  31.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  32.         boolean newbie = true;
  33.        
  34.         PrintWriter out = response.getWriter();
  35.        
  36.         Cookie[] cookies = request.getCookies();
  37.         if(cookies != null) {
  38.             for (Cookie c : cookies) {
  39.                 if ((c.getName().equals("repeatedVisitor") && c.getValue().equals("yes"))) {
  40.                     newbie = false;
  41.                     break;
  42.                 }
  43.             }
  44.         }
  45.        
  46.         String title;
  47.         if (newbie) {
  48.             Cookie returnedVisitorCookie = new Cookie("repeatedVisitor", "yes");
  49.             returnedVisitorCookie.setMaxAge(60 * 5);
  50.             response.addCookie(returnedVisitorCookie);
  51.             title = "Bine ai venit prima data!";
  52.         }
  53.         else {
  54.             title = "Bine ai revenit!";
  55.         }
  56.        
  57.         out.write("<title>" + title + "</title>");
  58.         out.write("</head><body></body></html>");
  59.     }
  60.  
  61.     /**
  62.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  63.      */
  64.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  65.         // TODO Auto-generated method stub
  66.         doGet(request, response);
  67.     }
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top