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
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
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.