<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
package a;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/* Setting MIME type for HTTP header */
response.setContentType("text/html");
/* Obtaining the output stream */
PrintWriter out = response.getWriter();
/* Memory variables that will hold the username and password */
String username = "";
String password = "";
/* Retrieving all unexpired cookies */
Cookie[] cookies = request.getCookies();
/* If cookies were retrieved */
if(cookies != null)
{
/* Traversing across all the cookies and extracting the username and password from the ValidUser and ValidPassword cookies respectively. */
for(int i=0; i < cookies.length; i++)
{
Cookie thisCookie = cookies[i];
if (thisCookie.getName().equals("ValidUser"))
{
username = thisCookie.getValue();
}
else if(thisCookie.getName().equals("ValidPassword"))
{
password= thisCookie.getValue();
}
}
}
/* Generating the actual Login form user interface */
out.println("<HTML><HEAD><TITLE>Login Form</TITLE></HEAD>");
out.println("<BODY><FORM ACTION='/autologin/s' METHOD='get' NAME='frmLogin'>");
out.println("<TABLE><TR><TD ALIGN='center'><IMG SRC='/MyWebApplication/security.jpg' WIDTH='64' HEIGHT='64' BORDER='0'>");
out.println("<BR>Welcome to Sharanam Shah's Website!<BR>Use a valid username and password <BR>");
out.println("to gain access to Sharanam Shah's Website</TD><TD BORDERCOLOR='#DEEFF9'>");
out.println("<TABLE WIDTH='25%' BORDER='1' ALIGN='center' CELLPADDING='3' CELLSPACING='1' BORDERCOLOR='#000000'>");
out.println("<TR BORDERCOLOR='#92CAEB' BGCOLOR='white'><TD COLSPAN='2'>Member Login</TD></TR>");
out.println("<TR BORDERCOLOR='#E6F3FB'><TD ALIGN='right'>Username:</TD><TD>");
out.println("<INPUT NAME='txtusername' TYPE='text' TABINDEX='1' VALUE='" + username + "' SIZE='15' MAXLENGTH='15'></TD>");
out.println("</TR><TR BORDERCOLOR='#E6F3FB'><TD ALIGN='right'>Password:</TD><TD>");
out.println("<INPUT NAME='txtpassword' TYPE='password' TABINDEX='2' VALUE='" + password + "' SIZE='15' MAXLENGTH='15'></TD></TR>");
out.println("<TR BORDERCOLOR='#E6F3FB'><TD COLSPAN='2' ALIGN='right'>");
out.println("<INPUT NAME='chkrem' TYPE='checkbox' VALUE='REMEMBER' TABINDEX='3'>REMEMBER ME</TD></TR>");
out.println("<TR BORDERCOLOR='#E0EEF7'><TD COLSPAN='2' ALIGN='right'>");
out.println("<INPUT NAME='submit' TYPE='submit' VALUE='Sign In' TABINDEX='4'></TD></TR>");
out.println("</TABLE></TD></TR></TABLE></FORM></BODY></HTML>");
/* Closing the output stream */
out.close();
}
}
"SetMe.java"
package a;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
public class SetMe extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/* Setting MIME type for HTTP header */
response.setContentType("text/html");
/* Obtaining the output stream */
PrintWriter out = response.getWriter();
/* Creating a Connection and Statement object for later use */
Connection con = null;
Statement stmt;
try
{
/* Memory variables that hold Db login information */
String username = "root";
String password = "sct2306";
String url = "jdbc:mysql://localhost/bookdb";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,username,password);
/* If the connection is successful */
if(con != null)
{
out.println("<BR>Successfully connected to MySQL server using TCP/IP...<BR>");
stmt = con.createStatement();
/* A query that will determine if the username and password captured are valid */
String query = "SELECT * FROM login WHERE userid = '" + request.getParameter("txtusername").toString() + "' AND password ='" + request.getParameter("txtpassword").toString() + "'";
/* Executing the query using the ResultSet object */
ResultSet rs = stmt.executeQuery(query);
/* If the resultset holds no records */
if(rs.next() == false)
{
out.println("<BR>Sorry. Invalid Login. Please try again.");
}
/* If the resultset holds records */
else
{
out.println("<BR>Successful Login. Welcome to the world of Servlets.");
/* If the chkrem i.e. Remember Me check box is checked on */
if(request.getParameter("chkrem") != null)
{
/* A cookie to hold the username is created */
Cookie returnLoginUser = new Cookie("ValidUser", request.getParameter("txtusername").toString());
/* Setting how much time in seconds should elapse before the cookie expires */
returnLoginUser.setMaxAge(36000);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginUser);
/* A cookie to hold the password is created */
Cookie returnLoginPassword = new Cookie("ValidPassword", request.getParameter("txtpassword").toString());
/* Setting how much time in seconds should elapse before the cookie expires */
returnLoginPassword.setMaxAge(36000);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginPassword);
}
/* If the chkrem i.e. Remember Me check box is checked off */
else
{
/* Assigning a null value */
Cookie returnLoginUser = new Cookie("ValidUser", "");
/* Deleting the cookie */
returnLoginUser.setMaxAge(0);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginUser);
/* Assigning a null value */
Cookie returnLoginPassword = new Cookie("ValidPassword", "");
/* Deleting the cookie */
returnLoginPassword.setMaxAge(0);
/* Adding cookie to the Set-Cookie response header by means of the addCookie method of HttpServletResponse */
response.addCookie(returnLoginPassword);
out.println("<BR><BR>You did not choose to remember the login information. Hence you will need to enter login details the next time you visit us.");
}
}
}
/* Closing the output stream */
out.close();
}
catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
}
}
}
Compile And Run on this Project for Better Understanding.
If this information is important to you , and you want to , I will continue writing some more details About Java, Servlet, Android Programming Language, so click on the advertising, available on this page. This is motivate me for writing some more useful stuff, Thank's
Comments
Post a Comment