Sunday, April 19, 2009

Write a program to display a login page to the user

Write a program to display a login page to the user. The login page should ask user to enter the account Id and pin number. The account id and pin number should be compared with the data in database, and should be validated. Display appropriate message to the user after validating the user input to the login form.

The files used to run the application are:

1. main.jsp
2. Login.jsp
3. process2.jsp
4. success.jsp
5. retry.jsp

Solution:
main.jsp

<html>
<head>
<title> </title>
</head>
<body>
<br>
<br><br>
<form action="process2.jsp” method = "post" >
<center>Account Id</center>
<input type = "text" name="acc_id>
<center>Pin Number</center>
<input type = "Pin Number" name = "pin_num">
<center><input type="submit" name="Submit" value="Login"></center>
</form>
</body>
</html>

Login.java

package Java_class;
import java.sql.*;
public class Login
{
private String account_id = "";
private String pin_number = "";
public Login()
{
}
public void setaccount_id(String acc_id)
{
this.acc_id = acc_id;
}
public void setPin_num(String pin_number)
{
this.pin_number = pin_number;
}
public boolean authenticate(String acc_id2, String pin_num2)
{
String query="select * from Registration";
String Dbacc_id="";
String DbPin_num="";

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:regist
er");
Statement stat=con.createStatement();
ResultSet rst=stat.executeQuery(query);
while(rst.next())
{
Dbacc_id=rst.getString("account_id");
DbPin_num=rst.getString("pin_number");

if (acc_id2.equals(Dbacc_id) &&
pin_num2.equals(DbPin_num))
{
return true;
// break;
}
}
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}


process2.jsp

<%@ page import="java.util.*" %>
<jsp:useBean id="idHandler" class="Java_class.Login" scope="request">
<jsp:setProperty name="idHandler" property="*"/>
</jsp:useBean>

<%
String username = request.getParameter("account_id");
String password = request.getParameter("pin_number");

// If authenticated pass control to success.jsp
if (idHandler.authenticate(account_id, pin_number))
{
%>
<jsp:forward page="success.jsp"/>
<%
} else {
%>
<jsp:forward page="retry.jsp"/>
<%
}
%>
//success.jsp
<html>
<head>
<title> User Validation Page </title>
</head>
<body>
You have successfully logged in to our Website
</body>
</html>

retry.jsp

<html>
<head>
<title> User Validation Page </title>
</head>
<body>
Incorrect username or password!!!!
<A href="main.jsp"> Retry </A>
</body>
</html>

The output of the program is as shown in Figure 14.1.

Figure 14.1: Login Form

After entering valid details, when the user clicks on Login button, a message is displayed to the user as shown in Figure 14.2.

Figure 14.2: Logon Success Message

If the user enters invalid details in the login form, a message is displayed to the user informing that the details entered are invalid. The message appears as shown in Figure 14.3.

Figure 14.3: Invalid Login Message

0 nhận xét: