.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Login Page in Java Server Page

Posted By:satyapriyanayak       Posted Date: September 14, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know how to do a simple login page using jsp.
 

In this login page we use java script for validation of blank form. Here we use a html page where user can enter his/her credentials (userid and password) and after submitting the button action goes to the doLogin.jsp page where data is being checked in the database and the valid user name is shown as output in welcome.jsp page.

Steps to follow for connecting to DBMS

1.Load the driver class into runtime environment by using forName() method of java.lang class.This method accepts a string parameter to contain the class name.This method throws java.lang class not found exception. Class is a predefined class and forName is the static method of the class, which is used to load the driver in the memory for connectivity.
Ex:- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

2.Create a reference of java.sql.connection by using getConnection() method of java.sql. DriverManager class.This method access three parameters as path of the driver in JDBC:ODBC bridge.We required DSN(Data source name) in this position.Dsn can be created in control panel

Ex: Connection con=DriverManager.getConnection("jdbc:odbc:dsnname","system","password");

The getConnection() method throws java.sql.sql exception.

3.Create an object of java.sql statement by using create statement () method of connection. Statement is being used to execute various sql commands.
Ex:- Statement stmt=con.createStatement();

4.Use appropriate method of statement to execute sql command for select command use executeQuery() method and for insert, update and delete use executeUpdate() method.


Creation of dsn(database source name) for Oracle
 
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

Note:- Here Username=system, Password=pintu and Dsn name=dsn1

Table creation with values

Create table login (userid varchar(50),password varchar(50))

insert into login values('Raj Kumar','raj123')
insert into login values('Ravi Kumar','ravi123')
insert into login values('Rahul Kumar','rahul123')

login.html

<html>
<head>
<script type="text/javascript">
function ccheck()
{
uid=document.f1.uid.value;
cpass=document.f1.cpass.value;

if(uid=="" || uid==null)
{
alert("Plz. Enter Your User ID");
document.f1.uid.focus();
return false;
}
if(cpass=="" || cpass==null)
{
alert("Plz. Enter Your Password");
document.f1.cpass.focus();
return false;
}
return true;
}
</script>
<title>Login Page in Jsp</title>
</head>
<body onload="document.f1.uid.focus()">
<h2>LOGIN PAGE IN JSP</h2>
<form id="f1" name="f1" action="doLogin.jsp" method="post" onsubmit="return ccheck()">
<table>
<tr>
<td><b>User ID</b></td>
<td><input name="uid" type="text"/></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input name="cpass" type="password"/></td>
</tr>
<tr>
<td><input  type="submit"  value="Submit" /> </td>
<td><input  type="reset"  value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>

doLogin.jsp

<%@ page import="java.sql.*" %>
<%! String cid="",pass="";Connection con; %>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs;
cid=request.getParameter("uid");
pass=request.getParameter("cpass");
//System.out.println("cid="+cid+"  pass="+pass);
rs=stmt.executeQuery("select * from login where userid='"+cid+"' and password='"+pass+"' ");
if(rs.next())
{
session.setAttribute("scid",cid);
con.close();                                                                   
response.sendRedirect("welcome.jsp");
}
else
{
%>
<html>
<body>
<h1>Invalid UserID or Password</h1>
<jsp:include page="login.html" />
<%
}
%>
</body>
</html>

welcome.jsp

<%@ page import="java.sql.*" %>
<%! String scid=""; %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Login Page</title>
</head>
<body>
<%
scid=(String)session.getAttribute("scid");
%>          
<div id="welcome">
<h2><span>Welcome:::<strong><font color='Red'><%= scid %></font></strong></span></h2>   
</div>
</body>
</html>

Running the application

Run the tomcat then write the below link in the Url
http://localhost:8081/jsp/

Here jsp is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.
After giving the url a set a listing will come, here only three appears
As login.html, doLogin.jsp and welcome.jsp.


 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend