SHARE
TWEET

e1400500_kotiteht4

a guest Dec 13th, 2016 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fi.vamk.e1400500;
  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. *
  15. * 1.  NAME
  16. *     LoginCookie.java
  17. * 2.  DESCRIPTION
  18. *     Basic html login form
  19. * 3.  VERSIONS
  20. *       Original:
  21. *         13.12.2016 / Sami Kärkkäinen
  22. *
  23. *       Version history:
  24. *       $Log$
  25. *
  26. **********************************************************************/
  27.  
  28.  
  29. /**
  30.  * Servlet implementation class LoginCookie
  31.  */
  32. @WebServlet("/LoginCookie")
  33. public class LoginCookie extends HttpServlet {
  34.     private static final long serialVersionUID = 1L;
  35.        
  36.     /**
  37.      * @see HttpServlet#HttpServlet()
  38.      */
  39.     public LoginCookie() {
  40.         super();
  41.         // TODO Auto-generated constructor stub
  42.     }
  43.  
  44.     /**
  45.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  46.      */
  47.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48.         Cookie[] cookies = request.getCookies();
  49.         PrintWriter out = response.getWriter();
  50.         String username = "";
  51.         String password = "";
  52.        
  53.         if(cookies!=null){
  54.             for(int i=0;i<cookies.length;i++){
  55.                 Cookie c = cookies[i];
  56.                 if(c.getName().equals("username")){
  57.                     username = c.getValue();
  58.                 }
  59.                 if(c.getName().equals("password")){
  60.                     password = c.getValue();
  61.                 }
  62.             }
  63.         }      
  64.         out.println("<form method=post>Username:<input type=text name=username value='" + username +"'></br>Password:<input type=password name=password></br><input type=submit value=Login></form>"); 
  65.     }
  66.  
  67.     /**
  68.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  69.      */
  70.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  71.         String username = request.getParameter("username");
  72.         String password = request.getParameter("password");
  73.         Cookie userCookie = new Cookie("username", username);
  74.         Cookie passwordCookie = new Cookie("password", password);
  75.        
  76.         userCookie.setPath("/");
  77.         passwordCookie.setPath("/");
  78.         userCookie.setMaxAge(7*24*60*60);
  79.         passwordCookie.setMaxAge(7*24*60*60);
  80.         response.addCookie(userCookie);
  81.         response.addCookie(passwordCookie);
  82.        
  83.         PrintWriter out = response.getWriter();
  84.         out.println("<h2>Tervetuloa " + username + "!</h2><form method=get><input type=submit value=Logout></form>");
  85.     }
  86. }
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
 
Top