JavaBean

Eclipse Helios/GlashFish/SQLite

JSP Login

System Requirements:

Download Eclipse Helios

http://www.eclipse.org/helios/

Download GlassFish Server 3.1

http://glassfish.java.net/downloads/3.1-final.html


Download SQLite

http://www.sqlite.org/download.html


Download SQLiteJDBC jar file

http://code.google.com/p/sqlite-jdbc/



Introduction:

Build a connection to a SQLite database located on C Drive using a JavaBean and a database connector class. Once connection is established, load specific user to JavaServer Pages Application, serviced by GlassFish Web Server; user is remains on login page if credentials do not check out.


Disclaimer: Information contained in the following is presented as is. This tutorial assumes you have basic programming knowledge. All tutorials are based on an Eclipse/Eclipse-based software. Should you need to familiarize yourself with a certain Eclipse environment, prior to continuing this tutorial, please stop now and see our Tutorials page...


Write Java classses to login

At this point we assume Eclipse Helios, SQLite database and JDBC jar file, GlassFish Web Server are downloaded/installed and two Java classes have been created. You are going to use a database connector class with JavaBean to authenticate against SQLite database. Copy and paste below code to your environment for each code samples.



DBConnector.java


[CODE]



/**

* Copyright 2012 Dököll Solutions, Inc.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

*

*

* @AppName: login

* Program: DBConnector.java

* Created from Copy: 2009.11.03.1.17.PM

*

* Modified: 2011.08.28.3.55.PM

* Added SQLite connection

*/



package com.dokoll.solutions.inc.developement.Utils;



/**

*

* @author Dököll Solutions, Inc.

* @version 2009.11.03.1.17.PM

*/


import java.sql.*;


//load program


public class DBConnector {

//get a connection

public static Connection getConnection() {

System.out.println("Connection Started...");

//declare and initialize

Connection con = null;

//entering try catch

try {

//load driver class

Class.forName("org.sqlite.JDBC").newInstance();

System.out.println("Class obtained...");

//connect to SQLite

con = DriverManager.getConnection("jdbc:sqlite:C:\\temp\\WEB_FILES\\SQLite\\sqlite-3_7_3\\Test.db");

//...

System.out.println("Connection fowarded...");

} catch (Exception e) {

e.printStackTrace();

}

//...

return con;

}

/*public static void main(String[] args) throws Exception {

//Use this block to test as Java program

getConnection();

System.out.println("Program Successful...");

}*/

}


Bean it!

Your JavaBean has been created, and we assume it compiles okay. You are now ready to plug in the connector class to complete login process. You are going to need a JSP page to submit request to login, another JSP page to pass the credentials to the JavaBean, lastly build one additional page if login is successful. Copy and paste below to your environment, areas of interest have been highlighted for your convenience.


LoginBean.java


/**

* Copyright 2012 Dököll Solutions, Inc.

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

*

*

* @AppName: login

* Program: LoginBean.java

* Created from Copy: 2010.09.05.11.01.AM

*

* Modified: 2012.04.07.5.36.AM

* Added SQLite connection

*/

package com.dokoll.solutions.inc.developement.Utils;


/**

* @author Dököll Solutions, Inc.

* @version 2010.09.05.11.01.AM

*

*/


import java.sql.*;



public class LoginBean {

// Declare login variables

String UserID;

String PassWord;


public LoginBean() {

//...

}


/**

* @return the userID

*/

public String getUserID() {

return UserID;

}


/**

* @param userID the userID to set

*/

public void setUserID(String userID) {

UserID = userID;

}


/**

* @return the passWord

*/

public String getPassWord() {

return PassWord;

}


/**

* @param passWord the passWord to set

*/

public void setPassWord(String passWord) {

PassWord = passWord;

}


// Button code, retrieve only if condition is met

// TO DO: Add HTML/JSP pages to talk to this JavaBean

public boolean authenticateUser() {

// Enter Try Catch

System.out.println("Entering Try catch Block...");

try {

System.out.println("Entered Try Catch...");

//Declare and initialize variables

PreparedStatement preparedStmt=null;

//bring in connection from DBConnector class

Connection con = DBConnector.getConnection();

System.out.println("DataSource Obtained, connection made...");

//set up query string (as a PreparedStatement)

String query= "SELECT * FROM USER_LOGINS WHERE UserName=? AND Password =?";

preparedStmt = con.prepareStatement(query);

//Grab values from user, pass to program

preparedStmt.setString(1,UserID);

preparedStmt.setString(2,PassWord);

//run query

ResultSet resultSt = preparedStmt.executeQuery();

System.out.println("Query ran...");

//do stuff based on user access

while (resultSt.next()) {

System.out.println("searching resultSet...");

//Load the results to these variables to begin comparing

//values entered against values received

String DBUserName = resultSt.getString("UserName");

String DBPassword = resultSt.getString("Password");

//2012.04.10.10.29.PM

//TO DO: Log userID, time in, time out for debugging purposes

//compare user submission against values returned from SQLite

if (UserID.equalsIgnoreCase(DBUserName)

&& PassWord.equalsIgnoreCase(DBPassword)) {

//...

System.out.println("It's a hit!");

//...

//load the appropriate JSP

return true;

}

}


} catch (Exception e) {

e.printStackTrace();

}

System.out.println("Hummm... Let's take a look at that!");

//...

//load the appropriate JSP

return false;


}


}


Build login.jsp file

Full code added below, jump ahead if necessary, areas of interest have been highlighted for your convenience.



login.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page session="false"%>


<html>

<head>



<SCRIPT LANGUAGE="JavaScript">

if (window != top) top.location.href = location.href;

</SCRIPT>


<title>Dököll Solutions, Inc. - Login</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


</head>


<body><font color="maroon" size=4>Welcome to Dököll Solutions, Inc.</font><hr width="100%" align=left></font><br><br>

<center>

<form name="form" method="post" action="home.jsp">


<table align="center">

<tr>

<td>

<h3><font color="navy">Enter Credentials to Log in</font></td>

</tr>

</table>

<br>


<table width="300px" align="center"

style="border:1px solid #000000;background-color:#efefef;">

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td>Username</td>

<td><input type="text" name="UserID" value=""></td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="Password" value=""></td>

<tr>

<td></td>

<td><input type="submit" name="Submit" value="Submit"> <input

type="reset" name="Reset" value="Reset"></td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

</table>


</form>


<table width="300px" align="center"

style="border:0px solid #000000;background-color:#efefef;">


<tr>

<td>

<center><a href="UserComments.jsp">Technical

Support</a> || <a href="login.jsp">Change

Password</a><br><br><CENTER><FONT size="-1"> <A href="LoggingOut.jsp">CANCEL</A> </FONT><br><br></CENTER></center>

</td>


</tr>


</table>

<center><font size="1" color="red"><b><a

href="SiteVersionInfo.jsp">Copyright © Dököll Solutions, Inc. 2012</a> <br>

All rights reserved</b></font></center>



<center>



</center>

</body>

</html>


Build home.jsp file

Full code added below, jump ahead if necessary, areas of interest have been highlighted for your convenience. This jsp page serves up the credentials to LoginBean.java and redirects to specific pages based on legality of the user's access.



home.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@page import="java.util.*" %>



<%

String name = request.getParameter( "UserID" );

session.setAttribute( "theName", name );

%>


<jsp:useBean id="idhandler" class="com.dokoll.solutions.inc.developement.Utils.LoginBean" scope="request">

<jsp:setProperty name="idhandler" property="*"/>

</jsp:useBean>

<%if (idhandler.authenticateUser()){

%>

<jsp:forward page="SearchComplaints.jsp"/>

<% } else { %>

<jsp:forward page="login.jsp"/>

<% } %>



Build SearchComplaints.jsp file

Full code added below, jump ahead if necessary, areas of interest have been highlighted for your convenience.



SearchComplaints.jsp


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@ page import="java.text.*,java.util.*" session="true"%>

<%@page import="java.sql.Date.*"%>

<html>

<head>


<%

String name = request.getParameter( "UserID" );

session.setAttribute( "theName", name );

%>


<%!


Date BeginDate=null;

Date EndDate=null;


SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

String DateOpen = format.format(new Date());

%>


<%

if (session.getAttribute("theName" ) == null) {

response.sendRedirect("Login.jsp");

return;

}


%>


<title>Logged in Successfully Page</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head>



<body bgcolor="#deefef">

<b>Logged in As:</b> <%= session.getAttribute( "theName" ) %>


</body>

</html>


Conclusion:

You can now have users login to SQLite database via a JavaServer Pages form, send users to specific pages based on access.


Hint: You can test the user's session by attempting to reach SearchComplaints.jsp page directly, you should be bounced back to login.jsp. This item can be linked to an existing JSP project, building Reports and Charts using JasperReports...


Questions, comments, please post a brief message on our Contact form on the main site. Thank you for coming...