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

Top 5 Contributors of the Month

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')


<script type="text/javascript">
function ccheck()

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


<%@ page import="java.sql.*" %>
<%! String cid="",pass="";Connection con; %>
Statement stmt=con.createStatement();
ResultSet rs;
//System.out.println("cid="+cid+"  pass="+pass);
rs=stmt.executeQuery("select * from login where userid='"+cid+"' and password='"+pass+"' ");
<h1>Invalid UserID or Password</h1>
<jsp:include page="login.html" />


<%@ 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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Login Page</title>
<div id="welcome">
<h2><span>Welcome:::<strong><font color='Red'><%= scid %></font></strong></span></h2>   

Running the application

Run the tomcat then write the below link in the Url

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:


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