Results 1 to 4 of 4
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Unhappy Working with Cookies and Session tracking(?)

    Hi!

    I do have this activity of mine where we have something like a 'username' and 'password'. function. the idea is, once you've inputted data for username and password in the login page, you just need to incorporate the user with cookies - upon clicking the submit button.

    Here's the thing. once he logged in once and closed the browser, and if he decides to enter your page, he can automatically logged in that page.

    i searched for it in the net but i cant seem to find one solution.

    what i have here are:
    Login.java
    Welcome.java.

    Login.java

    Java Code:
    package HtmlLogin;
    
    import javax.servlet.*; 
    import javax.servlet.http.*;
    import java.io.*;
    
    public class Login extends HttpServlet
    {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException
    	{	/*
    		 *Function Name: doGet
    		 *Description:    
    		 *			Displays the Login Page. 
    		 *Algorithm:			
    		 *Arguments: The function first sets the content type to text/html. 
    		 *
    		 *
    		 *Return type: none
    							 
    		 */
    		
    		
    		
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    	/*	
    	 * This was supposed to be my code for searching for cookies. 
    	 * request.
            Cookie[] cookies = request.getCookies();
            boolean foundCookie = false;
            boolean foundCookie2 = false;
            String x = "fail";
         
          
            if(cookies != null)
            {
            for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) 
            { 
                Cookie cookie1 = cookies[loopIndex];
                if (cookie1.getName().equals("MigzLottoUsername"))
                { 	foundCookie = true;
                	x = "Success";
                	break;
                	
                }
            }  
    
            for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) 
            { 
               
                Cookie cookie2 = cookies[loopIndex];
                if (cookie2.getName().equals("MigzLottoPassword"))
                { 	foundCookie2 = true;
                	x = "Success 2";
                	break;
                }
            }  
            }
       
    		*/
    		out.println("<html>");
    		out.println("	<head>");
    		out.println("		<title>Login Page</title>");
    		out.println("	</head>");
    		out.println("	<body background = \"images\\dancing__zoso_zoso.gif\" bgcolor=\"Black\">");
    		out.println("	<h1 align = \"center\"><font color=\"White\" face=\"arial\" size=\"5\">Log in to Led Zeppelin Lotto Game..." + x  + "</font></h1>");
    		out.println("   <table width=\"100%\" border=\"0\">");
    		out.println("	<tr>");
    		out.println("	<td><div align=\"center\"><br>");
    		out.println("	<form action=\"Welcome\" method=\"GET\">");
    		out.println("	<font color=\"White\" face=\"arial\" size=\"2\"> " +
    				"              Enter Username: <input type =\"text\"");
    		out.println("			name = \"username\"");
    		out.println("			value = \"\"");
    		out.println("			size = \"30\"");
    		out.println("			<br>");
    		out.println("			<br>");
    		out.println("			Enter Password: <input type = \"password\"");
    		out.println("			name = \"password\"");
    		out.println("			value = \"\"");
    		out.println("			size = \"30\"  </p>");
    		out.println("			<p>");
    		out.println("		<input type = \"submit\" value = \"Log-in\"");
    		out.println("		</p></form>");
    		out.println("  		 </div></td>");
    		out.println("		</tr>");
    		out.println("		</table>");
    		out.println("	</body>");
    		out.println("</html>");
    	    }
            
       
    	
    	}
    Welcome.java

    Java Code:
    package HtmlWelcome;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import java.io.*;
    
    
    public class Welcome extends HttpServlet
    {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException
    	{
    		
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		 //something
    		String strUsername = request.getParameter("username");
    		String strPassword = request.getParameter("password");
    		
    		Cookie setCookie = new Cookie("MigzLottoUsername", strUsername);
    		setCookie.setMaxAge(2592000);
    		setCookie.setPath("/MigzMP3");
    		response.addCookie(setCookie);
    		
    		Cookie setCookie2 = new Cookie("MigzLottoPassword", strPassword);
    		setCookie2.setMaxAge(2592000);
    		setCookie2.setPath("/MigzMP3");
    		response.addCookie(setCookie2);
    		
    		if ((strUsername.equalsIgnoreCase("admin") && strPassword.equalsIgnoreCase("password")) || (strUsername.equalsIgnoreCase("Migz") && strPassword.equalsIgnoreCase("Zeppelin")))
    		{
    
    			
    			out.println("<html>");
    			out.println("	<head>");
    			out.println("		<title>Welcome to Migz's Led Zeppelin Lotto Game</title>");
    			out.println("	</head>");
    			out.println("	<body background =\"images\\Led_Zeppelin_Iv_by_c00lryguy.jpg\" BGPROPERTIES=absolute>");
    			out.println("	<h1 align = \"center\"> <font><font color=\"White\" face=\"arial\" size=\"5\">Welcome! : "+ strUsername + "!</font></h1>");
    			out.println("	<font color=\"White\" face=\"arial\" size=\"2\">");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			out.println("<p>Information Information</p>");
    			
    
    			
    			out.println("	</font>");
    			out.println("	</body>");
    			out.println("</html>");
    
    
    		}
    		else if(strUsername.equals("") || strPassword.equals(""))  
    		{
    			out.println("<html>");
    			out.println("	<head>");
    			out.println("		<title>Access Denied</title>");
    			out.println("	</head>");
    			out.println("	<body background =\"images\\Led_Zeppelin_Iv_by_c00lryguy.jpg\" BGPROPERTIES=absolute>");
    			out.println("	<font color=\"Red\" face=\"arial\" size=\"4\">");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    
    			out.println(	"<p align = \"Center\"><img src = \"images\\warningL.JPG\" border=\"0\" width=\"115\" height=\"115\" ALIGN=MIDDLE >The Username or Password field must not be empty" +
    					"" + "<img src = \"images\\warningL.JPG\" border=\"0\" width=\"115\" height=\"115\" ALIGN=MIDDLE></p>");
    			out.println("	</font>");
    			out.println("	</body>"); 
    			out.println("</html>");  
    
    			
    		}
    		else  
    		{
    			out.println("<html>");
    			out.println("	<head>");
    			out.println("		<title>Access Denied</title>");
    			out.println("	</head>");
    			out.println("	<body background =\"images\\Led_Zeppelin_Iv_by_c00lryguy.jpg\" BGPROPERTIES=absolute>");
    			out.println("	<font color=\"Red\" face=\"arial\" size=\"4\">");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    			out.println("<br>");
    
    			out.println(	"<p align = \"Center\"><img src = \"images\\warningL.JPG\" border=\"0\" width=\"115\" height=\"115\" ALIGN=MIDDLE >The Username or Password you have entered is invalid." +
    					"" + "<img src = \"images\\warningL.JPG\" border=\"0\" width=\"115\" height=\"115\" ALIGN=MIDDLE></p>");
    			out.println("	</font>");
    			out.println("	</body>"); 
    			out.println("</html>");  
    
    			
    		}
    		
    	}
    	
    	
    }

    am i doing fine? help me out pls.

    thanks.

  2. #2
    zoomix is offline Member
    Join Date
    Oct 2008
    Location
    Sweden, Gothenburg
    Posts
    21
    Rep Power
    0

    Default

    Apart from the fact that you probably shouldn't store the password in a cookie as plain-text your code seems to do what you want.

    Is there a problem? There's quite a lot of code to read up there and I can't be arsed to read all of it, so a pointer to where you think the problem is would be very helpful.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    need to incorporate the user with cookies
    Once the password is validated, no need to use it in a cookie.
    Create a unique string of some kind to use as the cookie and save it locally where it can be retrieved when needed. It could be associated with other info for the user. It could have other attributes like expiration date etc.

  4. #4
    Vijay Mishra is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    The best way is to save the cookie with defined expire Age

Similar Threads

  1. Tracking Values Using Objects
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 09:18 PM
  2. which one to opt http session or cookies
    By amu in forum New To Java
    Replies: 1
    Last Post: 10-04-2008, 10:16 AM
  3. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  4. how to expire a session in struts without using cookies
    By sathya_k_83 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-22-2007, 03:07 PM
  5. Download Tracking
    By ganeshsalian in forum Advanced Java
    Replies: 7
    Last Post: 07-30-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •