Creating A JSP Login
System With MYSQL Databse.
NOW CREATE index.jsp file.
Now Create ActionJsp.jsp File In Same Directory And Write The Necessary Code To Check Username And Password Is Correct OR Not.
<%
if(rs.next()){
- In this article we are going to learn that how to
create a simple JSP Login System With MYSQL Database.
- To create a login system using JSP and MySQL Database perform the following steps.
#Open your Eclipse IDE
- Create Dynamic Web Project
- Add MySql Connector Jar File.
- Now in the Web Content Directory Create a JSP Class
File with any Name here is ConnectionClassFile.jsp.
- Perform the necessary coding to connect to the MySql
Database.
- Job of this class file is to return the connection Object so its sub classes can use it and perform the database operation by including ConnectionClassFile.jsp.
ConnectionClassFile.jsp
<%@page
import="java.sql.*" %>
<%!
public class MyClass{
Connection conn=null;
public Connection MyConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
this.conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql@123","root","");
}
catch(Exception ex){
out.println("exception
occuered " + ex.getMessage());
}
return conn;
}
}
%>
NOW CREATE index.jsp file.
- index.jsp File In Same Directory.
- This index.jsp page is created for
User Interface.
- when we run our project index.jsp will
be displayed in browser if everything goes right.
- In this page we have created a text field and a
password field to take input from the users and a submit button.
- when user hit the submit button after filling the
username and password an action(here ActionJsp.jsp is
responsible to perform database operation) will be performed,the job of
this action JSP is to execute the query to check that the information
entered by user is Correct OR Not.
- If information is incorrect then display appropriate message and if it correct then perform necessary action.
index.jsp
<body>
<center>
<form action="ActionJsp.jsp"
>
Username :<input type="text"
name="username"/><br/><br/>
Password :<input type="password"
name="password"/><br/><br/>
<input
type="submit" value="Login"/>
</form>
</center>
</body>
Now Create ActionJsp.jsp File In Same Directory And Write The Necessary Code To Check Username And Password Is Correct OR Not.
Job Of ActionJsp.jsp
- Include the ConnectionClassFile.jsp to access its properties.
- Declare PreparedStatement ,Connection ,ResultSet,create the Object of MyClass and declare a String query variable inside declarative
tag for further uses.
- Extract the form parameters and store into variables
inside JSP scriplet tag.
- Write the necessary query to check the username and password entered by user is correct or incorrect,See the example.
ActionJsp.jsp
<%@ page
language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page
import="java.sql.*" %>
<%@ include
file="ConnectionClassFile.jsp" %>
<body>
<%!
PreparedStatement ps;
Connection cn;
ResultSet rs;
MyClass obj=new
MyClass();
String query;
%>
<%
try{
String
username=request.getParameter("username");
String password=request.getParameter("password");
query="select
password from login where username=? ";
cn= obj.MyConnection();
ps=cn.prepareStatement(query);
ps.setString(1, username);
rs=ps.executeQuery();
if(rs.next()){
if(rs.getString(1).equalsIgnoreCase(password)){
out.print("Welcome
"+username) ;
}
else{
out.print("Invalid
Passwpord");
}
}
else{
out.print("Invalid Username");
}
}
catch(Exception ex){
out.print ("Sorry an exception occuered
" + ex.getMessage());
}
%>
</body>
So in the above example
we are taking input from the user and extracting form parameter in
ActionJsp.jsp page and checking the username and password entered by user is
correct or incorrect by sql query.
If user entered correct
username and password then page will display welcome message in the our case.
That’s All
Hope It Will Help
You Good Luck ;-)

No comments