Login using Ajax, J2ee, Oracle 10g
Get Finest Laptops from AMAZON
Login.jsp
<%--
Document : index
Created on : 2 Dec, 2013, 6:43:57 PM
Author : snlkjha
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Know_your_employee.css.css">
<script type="text/javascript" src="know_your_employee.js"></script>
<style type = "text/css">
td{font-weight:bold;}
select{font-weight:bold;color:blueviolet;}
</style>
</head>
<body>
<div id="nav">
<div id="flip"><span id="t">Know Your Employee</div>
</div>
<div id="content">
<div id="errmsg"></div>
<div id="loginform">
<div id="tt">
<form method="post" autocomplete="off">
<table>
<tr>
<td><h2 align="center">Please Enter Login Details</h2></td>
</tr>
<tr>
<td><input type="text" name="username" id="username" class="textbox" placeholder="Enter Username" /></td>
</tr>
<tr>
<td><input type="password" name="password" id="password" class="textbox" placeholder="Enter Password" /></td>
</tr>
<tr>
<td align="center"><input type="button" id="login" class="b" value="Sign In" onclick="login1();"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
<p id="ff">Site Designed and Developed by Sunil Kumar Jha<br/>Copyright © 2014 KNOW YOUR EMPLOYEE. All rights reserved.</p>
</body>
</html>
Welcome.jsp
<%--
Document : Welcome
Created on : 2 Dec, 2013, 7:20:52 PM
Author : snlkjha
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="Know_your_employee.css.css">
<title>JSP Page</title>
</head>
<body>
<div id="nav">
<div id="flip"><span id="t">Know Your Employee</span><a href="#" >Welcome ${un}</a></div>
</div>
<div id="content"></div>
<p id="ff">Site Designed and Developed by Sunil Kumar Jha<br/>Copyright © 2014 KNOW YOUR EMPLOYEE. All rights reserved.</p>
</body>
</html>
login.js
function aaa(str) {
document.getElementById(str).style.visibility = "hidden";
}
function login()
{
//$('#ff').form('submit');
var xmlhttp;
var username = document.getElementById("username").value;
if (username === "") {
document.getElementById("errmsg").innerHTML = " The Username must be filled out.";
document.getElementById("errmsg").style.visibility = "visible";
return;
}
var password = document.getElementById("password").value;
if (password === "") {
document.getElementById("errmsg").innerHTML = " The Password must be filled out.";
document.getElementById("errmsg").style.visibility = "visible";
return;
}
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
if (xmlhttp.responseText.match("jsp") !== null)
window.location.assign(xmlhttp.responseText);
else {
document.getElementById("username").value = "";
document.getElementById("password").value = "";
document.getElementById("errmsg").innerHTML = xmlhttp.responseText;
document.getElementById("errmsg").style.visibility = "visible";
// setInterval("aaa('errmsg')", 5000);
}
}
};
xmlhttp.open("POST", "LoginServlet?username=" + username + "&password=" + password, true);
xmlhttp.send();
}
LoginServlet.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Connect;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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;
/**
*
* @author snlkjha
*/
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("Username="+username);
String password = request.getParameter("password");
System.out.println("Password="+password);
Connection con = null;
HttpSession session = request.getSession(true);
PrintWriter out = response.getWriter();
//========================== tasktoperform.jsp dropdownbox data ================//
try {
con = Connect.getInstance().getDBConnection();
session.setAttribute("un", username);
session.setAttribute("pwd", password);
Statement smt = con.createStatement();
String query = "select * from Users where username='"+username+"' and password='"+password+"'";
System.out.println("Query="+query);
ResultSet rs = smt.executeQuery(query);
if (rs.next()) {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Welcome.jsp");
out.println("Welcome.jsp");
}
else{
out.println("The Username/Password You have entered is incorrect, Please try again.");
}
} catch (SQLException ex) {
} catch (Exception e) {
}finally{ con.close();}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
Output:
<%--
Document : index
Created on : 2 Dec, 2013, 6:43:57 PM
Author : snlkjha
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Know_your_employee.css.css">
<script type="text/javascript" src="know_your_employee.js"></script>
<style type = "text/css">
td{font-weight:bold;}
select{font-weight:bold;color:blueviolet;}
</style>
</head>
<body>
<div id="nav">
<div id="flip"><span id="t">Know Your Employee</div>
</div>
<div id="content">
<div id="errmsg"></div>
<div id="loginform">
<div id="tt">
<form method="post" autocomplete="off">
<table>
<tr>
<td><h2 align="center">Please Enter Login Details</h2></td>
</tr>
<tr>
<td><input type="text" name="username" id="username" class="textbox" placeholder="Enter Username" /></td>
</tr>
<tr>
<td><input type="password" name="password" id="password" class="textbox" placeholder="Enter Password" /></td>
</tr>
<tr>
<td align="center"><input type="button" id="login" class="b" value="Sign In" onclick="login1();"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
<p id="ff">Site Designed and Developed by Sunil Kumar Jha<br/>Copyright © 2014 KNOW YOUR EMPLOYEE. All rights reserved.</p>
</body>
</html>
Welcome.jsp
<%--
Document : Welcome
Created on : 2 Dec, 2013, 7:20:52 PM
Author : snlkjha
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="Know_your_employee.css.css">
<title>JSP Page</title>
</head>
<body>
<div id="nav">
<div id="flip"><span id="t">Know Your Employee</span><a href="#" >Welcome ${un}</a></div>
</div>
<div id="content"></div>
<p id="ff">Site Designed and Developed by Sunil Kumar Jha<br/>Copyright © 2014 KNOW YOUR EMPLOYEE. All rights reserved.</p>
</body>
</html>
login.js
function aaa(str) {
document.getElementById(str).style.visibility = "hidden";
}
function login()
{
//$('#ff').form('submit');
var xmlhttp;
var username = document.getElementById("username").value;
if (username === "") {
document.getElementById("errmsg").innerHTML = " The Username must be filled out.";
document.getElementById("errmsg").style.visibility = "visible";
return;
}
var password = document.getElementById("password").value;
if (password === "") {
document.getElementById("errmsg").innerHTML = " The Password must be filled out.";
document.getElementById("errmsg").style.visibility = "visible";
return;
}
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
if (xmlhttp.responseText.match("jsp") !== null)
window.location.assign(xmlhttp.responseText);
else {
document.getElementById("username").value = "";
document.getElementById("password").value = "";
document.getElementById("errmsg").innerHTML = xmlhttp.responseText;
document.getElementById("errmsg").style.visibility = "visible";
// setInterval("aaa('errmsg')", 5000);
}
}
};
xmlhttp.open("POST", "LoginServlet?username=" + username + "&password=" + password, true);
xmlhttp.send();
}
Java Classes & Servlet
Connect.java
package Connect;
/**
*
* @author trainee
*/
import java.sql.*;
public class Connect {
Connection con = null;
static Connect _instance;
public static Connect getInstance() {
if (_instance == null) {
_instance = new Connect();
}
return _instance;
}
public Connection getDBConnection() throws ClassNotFoundException, SQLException {
String status = "";
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","sunil","sunil");
return con;
}
}
LoginServlet.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Connect;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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;
/**
*
* @author snlkjha
*/
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("Username="+username);
String password = request.getParameter("password");
System.out.println("Password="+password);
Connection con = null;
HttpSession session = request.getSession(true);
PrintWriter out = response.getWriter();
//========================== tasktoperform.jsp dropdownbox data ================//
try {
con = Connect.getInstance().getDBConnection();
session.setAttribute("un", username);
session.setAttribute("pwd", password);
Statement smt = con.createStatement();
String query = "select * from Users where username='"+username+"' and password='"+password+"'";
System.out.println("Query="+query);
ResultSet rs = smt.executeQuery(query);
if (rs.next()) {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Welcome.jsp");
out.println("Welcome.jsp");
}
else{
out.println("The Username/Password You have entered is incorrect, Please try again.");
}
} catch (SQLException ex) {
} catch (Exception e) {
}finally{ con.close();}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
Output: