Source Code of Facebook Login Mini Project

Details –¬†Facebook Login Mini Project

Tools – Tomcat , Notepad++

JDK installed , JDBC Driver , ServletAPI

Save as Style.css
html, body, div, span,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend {
	background: transparent;
	border: 0;
	margin: 0;
	padding: 0;
	vertical-align: baseline;
	font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;
}
body.login { background: url(bg.jpg); }
h2 { font-size: 30px; line-height:38px; font-weight:normal; }
.loginbox { background: #FBFCFD; padding: 10px; width: 490px; margin: 4% auto 0 auto; position: relative; }
.loginboxinner {
	background: #FBFCFD; padding: 5px; position: relative; border: 1px solid #FBFCFD;
	-moz-box-shadow: inset 0 1px 0 #FBFCFD; -webkit-box-shadow: inset 0 1px 0 #FBFCFD; box-shadow: inset 0 1px 0 #FBFCFD;
}
.loginheader { height: 20px; }
.loginform { margin: 4px auto; text-align:center; }

.loginbox h1 { font-size: 30px; letter-spacing: 1px; color: #555; font-weight: normal; padding-top: 10px; }
.loginbox p { margin: 7px 0 5px 0; }
.loginbox label { display: block; color: #666; letter-spacing: 1px; font-size: 18px; }
.loginbox input.mini { width:195px; }
.loginbox input {
	padding: 12px 10px;     color: #000;
	font-family: Arial, Helvetica, sans-serif; margin-top: 8px; font-size: 15px; border: 1px solid #ccc; width: 420px;  outline: none;
}
.loginbox button {
	background: #5870A8; background-image:-webkit-linear-gradient(top, #637BAD, #4F67A4); padding: 10px 20px; font-size: 18px; border: 1px solid #1D3871; letter-spacing: 1px; color: #fff; width: 440px; line-height:30px; font-family:	'Helvetica Neue', Helvetica, Arial, sans-serif;
	-moz-box-shadow: 1px 1px 3px #1D3871; -webkit-box-shadow: 1px 1px 3px #1D3871; box-shadow: 1px 1px 3px #1D3871; cursor: pointer;
}

.radius { -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; }
.radius1 { -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; }
.title { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color:#8E989E; line-height:28px; font-weight:normal; text-align:center; font-size:16px; }

@media screen and (max-width: 430px) {

	body { font-size: 11px; }
	button, input, select, textarea { font-size: 11px; }

	.loginbox { width: auto; margin: 10px; }
	.loginbox input { width: 95%; }
	.loginbox button { width: 100%; }
}	

#facebook-Bar {
	background:#45619D;
	border-bottom: 1px solid #0053A6;
	box-shadow: 0 0 2px rgba(0, 0, 0, 0.52);
	min-width: 1000px;
	width: 100%;
	z-index: 1000;
}
#facebook-Frame {
	margin-left:auto;
	margin-right:auto;
	max-width:1000px;
	height:70px;
	display:block;
}
/*-------LOGO-----------*/
#logo {
	float: left;
	height: 70px;
	position: relative;
	width: 160px;
}
#logo a {
	position: absolute;
	top: 20px;
	font-size:23px; font-weight:bold; color:#FFF;
	text-decoration:none;
	font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

/*-------Login-----------*/
#header-main-right {
	float: right;
	height: 68px;
	position: relative;
	top:5px;
}
#header-main-right-nav {
	padding: 4px 6px 4px 4px;
	position: absolute;
	right: 0;
}
#login_form .inputtext {
	background-color: #FFFFFF;
	height:16px;
	font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
	font-size: 13px;
	margin: 0;
	padding: 5px 8px 5px 8px;
	width: 166px;
	border:1px solid #3A518A;
}
#login_form .inputtext:hover {
	border-color: #A0A0A0 #B9B9B9 #B9B9B9;
	border-image: none;
	border-style: solid;
	border-width: 1px;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1) inset;
}

#login_form table tr td {
	padding: 0 0 0 5px;
}
#login_form table tr td label {
	color: #fff;
	cursor: pointer;
	font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
	font-size: 12px;
	font-weight: normal;
	padding-left: 1px;
	text-align: left;
	vertical-align: middle;
}
.fbbutton { border:1px solid #1D3871; width:94px; background-image:-webkit-linear-gradient(top, #637BAD, #4F67A4); font-size:14px; line-height:25px; font-weight:normal; color:#fff; }
Save as Facebook.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Facebook Style Home Page</title>
		<link rel="stylesheet" href="style.css" type="text/css" />
<style>
ul { margin: 20px; }

/* -------------------- Select Box Styles: bavotasan.com Method (with special adaptations by ericrasch.com) */
/* -------------------- Source: http://bavotasan.com/2011/style-select-box-using-only-css/ */
.styled-select {
   //background: url(http://i62.tinypic.com/15xvbd5.png) no-repeat 96% 0;
   height: 45px;
   overflow: hidden;
   width: 450px;
}
</style>

<script>


function validateemail() {
    var x = document.forms["myForm"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        return false;
    }
	
	 var xx = document.forms["myForm"]["email11"].value;
    var atpos = xx.indexOf("@");
    var dotpos = xx.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=xx.length) {
        alert("Not a valid e-mail address");
        return false;
    }
	if(x == xx)
	{
		
	}
	else
       {
		alert("Check Email Address Again.");
		return false;
	}

	 var ab = document.forms["myForm"]["passwords"].value;
	  var bc = document.forms["myForm"]["password1"].value;
	if(ab == bc)
	{
		
	}
	else
       {
		
       alert("PASSWORD NOT MATCHED");
		return false;
	}
}		


       </script>

</head>

<body class="login">

<!-- header starts here -->
<div id="facebook-Bar">
<div id="facebook-Frame">
<div id="logo"> <a href="#">Facebook</a></div>
<div id="header-main-right">
<div id="header-main-right-nav">
<form method="post" action="/examples/Flogin" id="login_form" name="login_form">
<table border="0" style="border:none">
<tr>
<td ><input type="text" tabindex="1"  id="email" placeholder="Email or Phone" name="email" class="inputtext radius1" value=""></td>
<td ><input type="password" tabindex="2" id="pass" placeholder="Password" name="pass" class="inputtext radius1" ></td>
<td ><input type="submit" class="fbbutton" name="login" value="Login" /></td>
</tr>
<tr>
<td><label><input id="persist_box" type="checkbox" name="checked" value="1" checked="1"/><span style="color:#ccc;">Keep me logged in</span></label></td>
<td><label><a href="" style="color:#ccc; text-decoration:none">forgot your password?</a></label></td>
</tr>
</table>
</form></div>
</div>
</div>
</div>
<!-- header ends here -->
<div class="loginbox radius">
<h2 style="color:#141823; text-align:center;">Welcome to Facebook</h2>
<div class="loginboxinner radius">
<div class="loginheader">
<h4 class="title">Connect with friends and the world around you.</h4>
</div>
<!--loginheader-->
<div class="loginform">
<div id="div"></div>
<form name="myForm" id="login" action="/examples/Fregistration" onsubmit="return validateemail();" autocomplete="off" method="post">
<%@ page import="javax.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.servlet.http.*" %>
<%@ page import="java.sql.*" %>
<%! public String str; %>
<%

response.setContentType("text/html");
    PrintWriter localPrintWriter = response.getWriter();
 HttpSession localHttpSession = request.getSession(false);

String sa;

String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
String DB_URL = "jdbc:oracle:thin:@localhost:1521:XE";

String USER = "system";
String PASS = "system";

   Connection conn = null;
   Statement stmt = null;
ResultSet rs = null;

Class.forName(JDBC_DRIVER);
conn =DriverManager.getConnection(DB_URL,USER,PASS);

DatabaseMetaData dbm = conn.getMetaData();
            rs = dbm.getTables(null, "SYSTEM", "SESSIONS", null);
             if (!rs.next()) {
                 System.out.println("NOT");
             }else{
                stmt=conn.createStatement();
String sql="select * from sessions";

System.out.println("Created Statement"+sql);

rs=stmt.executeQuery(sql);
rs.next();
sa=rs.getString("ses");
System.out.println("Done");
if(str==null)
{

String s1 = sa;
   HttpSession localHttpSessions = request.getSession();
	localHttpSessions.setMaxInactiveInterval(20*60);

        localHttpSessions.setAttribute("s1", s1);
}
else
{
localPrintWriter.println("<script type=\"text/javascript\">");  
localPrintWriter.println("alert('PLEASE LOGIN AGAIN');");  
localPrintWriter.println("</script>");
     String site = new String("http://localhost:8089/examples/Facebook.jsp");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site);
}

if (localHttpSession == null)
{
  String site = new String("http://localhost:8089/examples/Facebook.jsp");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site);
 } else {  

 str = (String)localHttpSession.getAttribute("s1");
      if(str=="null")

{ 

localPrintWriter.println("<script type=\"text/javascript\">");  
localPrintWriter.println("alert('PLEASE LOGIN AGAIN');");  
localPrintWriter.println("</script>");
     String site = new String("http://localhost:8089/examples/Facebook.jsp");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site);
}
else
{
	String site = new String("http://localhost:8089/examples/ProfileServlet");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 

             }

}
}

%>

                    <input type="text" id="username" name="username" placeholder="First Name" value="" class="radius mini" required/> <input type="text" id="username" name="lastname" placeholder="Last Name" value="" class="radius mini" required />

                    <input type="text"  onkeypress='phoneno()' id="contact"   maxlength="10" size="10"  min="0" max="10" name="contact" placeholder="Your Contact Number" value="" class="radius" required />

                    <input type="text" id="username" name="email" placeholder="Your Email" value="" class="radius" required />

                    <input type="text" id="password" name="email11" placeholder="Re-enter Email" class="radius" required/>

                    <input type="password" id="password" name="passwords" placeholder="New Password" class="radius" required/>
					<input type="password" id="password" name="password1" placeholder="Re-enter Password" class="radius" required/>
<ul>
				<select class="radius" name="securityques">
<option>what is your nationality?</option>
<option>what was your last mobile number?</option>
<option>what is your pet name?</option>
<option>what is your favourite food?</option>
</select></ul>
<input type="text" id="username" name="sque" placeholder="Your Security Answer" value="" class="radius" required/>

                	<input type="Submit"  class="radius title" style="background-color:#3b5998"  name="client_login" value="Sign Up for Facebook" />
</form></div>
<!--loginform--></div>
<!--loginboxinner--></div>
<!--loginbox-->

</body>

</html>

Save as Flogin.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Flogin extends HttpServlet
{

	public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		Connection cn;
		Statement st;
		ResultSet rs;
		String s,s1,s2,s11;
		s1=req.getParameter("email");
		s2=req.getParameter("pass");
		String[] s3=req.getParameterValues("checked");

		 if (req.getParameter("checked") == null)   // above jsp:expression gives 1 or null and now working...success
       		 {
        		//false
 try
    {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      System.out.println(localClassNotFoundException);
    }
    try
    {
      Connection localConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
      Statement localStatement = localConnection.createStatement();

      String str1 ="select *from facebook where email='" + s1 + "' OR contact='"+ s1 +"' and pass='" + s2 + "'";
      ResultSet localResultSet = localStatement.executeQuery(str1);
      if (localResultSet.next())
      {
        out.print("Welcome, " + s1);
	req.setAttribute("s1", s1);
out.print("

<hr>

");
      out.print("
<a href='LogoutServlet'>Logout</a>");

//forward the request to Servlet2
        req.getRequestDispatcher("Profile").include(req, res);  

              // res.sendRedirect("http://localhost:8089/examples/Profile");
      }
      else
      {
           res.sendRedirect("http://localhost:8089/examples/ProfileServlet");
      }
      localConnection.close();
      localStatement.close();
    }
    catch (SQLException localSQLException)
    {
      System.out.println(localSQLException);
    }

       		 } else {
//True
 try
    {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      System.out.println(localClassNotFoundException);
    }
    try
    {
      Connection localConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
      Statement localStatement = localConnection.createStatement();

      String str1 = "select *from facebook where email='" + s1 + "' OR contact='"+ s1+"' and pass='" + s2 + "'";
      ResultSet localResultSet = localStatement.executeQuery(str1);
      if (localResultSet.next())
      {
        out.print("Welcome, " + s1);
        HttpSession localHttpSession = req.getSession();
	localHttpSession.setMaxInactiveInterval(20*60);

        localHttpSession.setAttribute("s1", s1);
        res.sendRedirect("http://localhost:8089/examples/ProfileServlet");

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
      Statement stmt = conn.createStatement();
System.out.println("Creating table in given database...");
      stmt = conn.createStatement();
 String sql = "CREATE TABLE SESSIONS(SES VARCHAR(100))";
      stmt.executeUpdate(sql);
      System.out.println("Created table in given database...");
String sqll = "Insert  into SESSIONS(ses) values('" + s1 + "')";
  System.out.println("Statement executed " + sqll);
        stmt.executeUpdate(sqll);
      }
      else
      {
res.sendRedirect("http://localhost:8089/examples/ProfileServlet");
      }
      localConnection.close();
      localStatement.close();
    }
    catch (SQLException localSQLException)
    {
      System.out.println(localSQLException);
    }

	}
	}
}
Save as Fregistration.java
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Fregistration
  extends HttpServlet
{
  static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
  static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:XE";
  static final String USER = "system";
  static final String PASS = "system";

  public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws IOException, ServletException
  {
    String str1 = paramHttpServletRequest.getParameter("username");
    String str2 = paramHttpServletRequest.getParameter("lastname");
    String str3 = paramHttpServletRequest.getParameter("contact");
    String str4 = paramHttpServletRequest.getParameter("email");
    String str5 = paramHttpServletRequest.getParameter("passwords");
    String str6 = paramHttpServletRequest.getParameter("securityques");
    String str7 = paramHttpServletRequest.getParameter("sque");

    Connection localConnection = null;
    Statement localStatement = null;
    try
    {
      Class.forName("oracle.jdbc.driver.OracleDriver");

      localConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");

      System.out.println("Creating statement...");
      if (localConnection == null) {
        System.out.println("Connection failed");
      }
      localStatement = localConnection.createStatement();

      String str8 = "SELECT * from facebook where email='" + str4 + "'";
      System.out.println("Statement executed " + str8);
      ResultSet localResultSet = localStatement.executeQuery(str8);
      String str9 = null;
      while (localResultSet.next()) {
        str9 = localResultSet.getString("email");
      }
      if (str9 == null)
      {
        localStatement = localConnection.createStatement();

        String str10 = "Insert  into facebook(username,lastname,contact,email,pass,ques,ans) values('" + str1 + "','" + str2 + "','" + str3 + "','" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "')";
        System.out.println("Statement executed " + str10);
        localStatement.executeUpdate(str10);
paramHttpServletResponse.sendRedirect("http://localhost:8089/examples/Facebook.jsp");
      }
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }
}

Save as LogoutServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setContentType("text/html");
			PrintWriter out=response.getWriter();
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "system");
   			   Statement stmt = conn.createStatement();
				System.out.println("Creating table in given database...");
  				    stmt = conn.createStatement();
			 String sql = "DROP TABLE SESSIONS";
     			 stmt.executeUpdate(sql);
    			  System.out.println("DROPPED given database...");
		}
		catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }

			HttpSession session=request.getSession();
			session.invalidate();

			out.print("You are successfully logged out!");
			out.print("

<hr>

");
			out.print("
<a href='Facebook.jsp'>Login Page</a>");
			out.close();
	}

}

Save as Profile.java
import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Profile
  extends HttpServlet
{
  protected void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    paramHttpServletResponse.setContentType("text/html");
    PrintWriter localPrintWriter = paramHttpServletResponse.getWriter();

String str=paramHttpServletRequest.getAttribute("s1").toString();
 localPrintWriter.print("Hello, " + str + " Welcome to Profile");
      localPrintWriter.print("

<hr>

");
      localPrintWriter.print("
<a href='LogoutServlet'>Logout</a>");
    localPrintWriter.close();
  }
}

Save as ProfileServlet.java
import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ProfileServlet
  extends HttpServlet
{
  protected void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    paramHttpServletResponse.setContentType("text/html");
    PrintWriter localPrintWriter = paramHttpServletResponse.getWriter();

    HttpSession localHttpSession = paramHttpServletRequest.getSession(false);

    if (localHttpSession == null )
    {
  PrintWriter out = paramHttpServletResponse.getWriter();
paramHttpServletResponse.setContentType("text/html");
out.println("<script type=\"text/javascript\">");  
out.println("alert('PLEASE LOGIN AGAIN');");  
out.println("</script>");
      paramHttpServletRequest.getRequestDispatcher("Facebook.jsp").include(paramHttpServletRequest, paramHttpServletResponse);
    }
    else
    {
      String str = (String)localHttpSession.getAttribute("s1");
      if(str==null)
{
PrintWriter out =paramHttpServletResponse.getWriter();
paramHttpServletResponse.setContentType("text/html");
out.println("<script type=\"text/javascript\">");  
out.println("alert('PLEASE LOGIN AGAIN');");  
out.println("</script>");
      paramHttpServletRequest.getRequestDispatcher("Facebook.jsp").include(paramHttpServletRequest, paramHttpServletResponse);
}
else
{
 localPrintWriter.print("Hello, " + str + " Welcome to Profile");
      localPrintWriter.print("

<hr>

");
      localPrintWriter.print("
<a href='LogoutServlet'>Logout</a>");
}

    }
    localPrintWriter.close();
  }
}

Add this Lines to Web.xml
<web-app>
<servlet>
        <servlet-name>Profile</servlet-name>
        <servlet-class>Profile</servlet-class>
    </servlet>
	 <servlet-mapping>
        <servlet-name>Profile</servlet-name>
        <url-pattern>/Profile</url-pattern>
    </servlet-mapping>

<servlet>

         <servlet-name>ProfileServlet</servlet-name>
         <servlet-class>ProfileServlet</servlet-class>
     </servlet>

     <servlet-mapping>
         <servlet-name>ProfileServlet</servlet-name>
         <url-pattern>/ProfileServlet</url-pattern>
     </servlet-mapping>

<servlet>
         <servlet-name>LogoutServlet</servlet-name>
         <servlet-class>LogoutServlet</servlet-class>
     </servlet>

     <servlet-mapping>
         <servlet-name>LogoutServlet</servlet-name>
         <url-pattern>/LogoutServlet</url-pattern>
     </servlet-mapping>

<servlet>
	<servlet-name>Fregistration</servlet-name>
	<servlet-class>Fregistration</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Fregistration</servlet-name>
	<url-pattern>/Fregistration</url-pattern>
</servlet-mapping>
<servlet>
	<servlet-name>Flogin</servlet-name>
	<servlet-class>Flogin</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Flogin</servlet-name>
	<url-pattern>/Flogin</url-pattern>
</servlet-mapping>
</web-app>

Download Source Code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s