JSP Login System With Mysql Database

Share:
Creating A JSP Login System With MYSQL Databse.
  • In this article we are going to learn that how to create a simple JSP Login System With MYSQL Database.
  • Here in this Example we are checking for username and password.
  • 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

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database