Press enter to see results or esc to cancel.


Java Jsp MySql login Project Source Code

Java Jsp MySql login Project Source Code Download

In the video series I have shown step by step how to create a JAVA JSP login Project.

Java Jsp MySql login Project Part 1
https://www.youtube.com/watch?v=rC1kZewgb94

JSP login Form – Java Jsp MySql login Project Part 2
https://www.youtube.com/watch?v=Y1lOQ6mEixA

Configuring Sign In Controller Java Jsp MySql login Project Part 3
https://www.youtube.com/watch?v=NximJGEfAqc

Simple Page Navigation Java Jsp MySql login Project Part 4
https://www.youtube.com/watch?v=aioptdinv1w

Session Management – Java Jsp MySql login Project Part 5
https://www.youtube.com/watch?v=BUmbCcTtNPU

Enable Sign Out Of User-Java Jsp MySql login Project Part 6
https://www.youtube.com/watch?v=w6mzpSZ1UL4

Hide .jsp extension,Custom Url for JSP Pages-Java Jsp MySql login Project Part 7
https://www.youtube.com/watch?v=0wRlsAflLKI

First create a Dynamic Web Project in Eclipse.

Download the below jars and add in Class Path
1.javax.servlet-3.0.jar
2.mysql-connector-java-5.0.0-beta-bin

  1. Project Structure
    
    
  2. Login_Bean.java
    package bean;
    public class Login_Bean {
      private String user_name;
      private String password;
      public String getUser_name() {
        return user_name;
      }
      public void setUser_name(String user_name) {
        this.user_name = user_name;
      }
      public String getPassword() {
       return password;
      }
      public void setPassword(String password) {
       this.password = password;
      }
    }
    
  3. DB_Connection.java
    package common_things;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class DB_Connection {
      public static void main(String[] args) {
       DB_Connection ob_DB_Connection=new DB_Connection();
       System.out.println(ob_DB_Connection.getConnection());
      }
      public Connection getConnection(){
       Connection connection=null;
       System.out.println("Connection called");
      try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login_project","root", "root");
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (SQLException e) {
        e.printStackTrace();
      }
        return connection;
      }
    }
    
  4. Login_Modal.java
    package modal;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import bean.Login_Bean;
    import common_things.DB_Connection;
    public class Login_Modal {
      public boolean check_user_name(Login_Bean obj_Login_Bean){
      boolean flag=false;
      DB_Connection obj_DB_Connection=new DB_Connection();
      Connection connection=obj_DB_Connection.getConnection();
      PreparedStatement ps=null;
      ResultSet rs=null;
      try {
       String qurey="select * from admin where user_name=? and password=?";
       ps=connection.prepareStatement(qurey);
       ps.setString(1, obj_Login_Bean.getUser_name());
       ps.setString(2, obj_Login_Bean.getPassword());
       System.out.println(ps);
       rs=ps.executeQuery();
       if(rs.next()){
        flag=true;
       }
      } catch (Exception e) {
       / / TODO: handle exception
     }finally {
      try {
       if(connection!=null){
        connection.close();
       }
     } catch (Exception e2) {
     // TODO: handle exception
     }
     }
     return flag;
     }
    }
    
  5. Sign_in_controller.jsp
     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <%@page import="modal.Login_Modal"%>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
      </head>
      <body>
       <jsp:useBean id="obj_Login_Bean" class="bean.Login_Bean"></jsp:useBean>
       <jsp:setProperty property="*" name="obj_Login_Bean"/>
       <%
         System.out.println(obj_Login_Bean.getUser_name());
         System.out.println(obj_Login_Bean.getPassword());
         Login_Modal obj_Login_Modal=new Login_Modal();
         boolean flag=obj_Login_Modal.check_user_name(obj_Login_Bean);
        if(flag){
         session.setAttribute("user_session", obj_Login_Bean);
        %>
        <script type="text/javascript">
           window.location.href="http://localhost:8080/Login_Project/user-home-page/<%=obj_Login_Bean.getUser_name()%>";
        </script>
      <%
    }else{
     session.setAttribute("login_message", "Login Failed, User name and Password is Wrong");
      %>
      <script type="text/javascript">
       window.location.href="http://localhost:8080/Login_Project/index.jsp";
       </script>
      <%
     }
     %>
     </body>
     </html>
    
  6. Signoutcontroller.jsp
    <%
    session.removeAttribute("user_session");
    session.setAttribute("login_message", "Sign out Successfull");
    %>
    <script type="text/javascript">
    window.location.href="http://localhost:8080/Login_Project/index.jsp";
    </script>
    
  7. Home.jsp
    <%@page import="bean.Login_Bean"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
     <%
     Login_Bean obj_Login_Bean=(Login_Bean)session.getAttribute("user_session");
     if(obj_Login_Bean==null){
     session.setAttribute("login_message", "Please login first");
     %>
     <script type="text/javascript">
     window.location.href="http://localhost:8080/Login_Project/index.jsp";
     </script>
     <% 
     }else{
     %>
    <center>
    <h1>Home Page</h1>
    <table border="1">
    <tr>
    <td><a href="http://localhost:8080/Login_Project/user-home-page">Home</a> </td>
    <td><a href="http://localhost:8080/Login_Project/user-profile">Profile</a> </td>
    <td> Welcome <%=obj_Login_Bean.getUser_name() %></td>
    <td> 
    <a href="http://localhost:8080/Login_Project/Signoutcontroller">Log Out</a>
    </td>
    </tr>
    </table>
    <%
     }
    %>
    </center>
    </body>
    </html>
    
  8. My_Profile.jsp
    <%@page import="bean.Login_Bean"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
     Login_Bean obj_Login_Bean=(Login_Bean)session.getAttribute("user_session");
     if(obj_Login_Bean==null){
     session.setAttribute("login_message", "Please login first");
     %>
     <script type="text/javascript">
     window.location.href="http://localhost:8080/Login_Project/index.jsp";
     </script>
     <% 
     }else{
     %>
    <center>
    <h1>Profile Page</h1>
    <table border="1">
    <tr>
    <td><a href="http://localhost:8080/Login_Project/user-home-page">Home</a> </td>
    <td><a href="http://localhost:8080/Login_Project/user-profile">Profile</a> </td>
    <td> Welcome <%=obj_Login_Bean.getUser_name() %></td>
    <td> <a href="http://localhost:8080/Login_Project/Signoutcontroller">Log Out</a></td>
    </tr>
    </table>
    </center>
    <% 
     }
     %>
    </body>
    </html>
    
  9. index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <center>
    <h3>Login Here</h3>
    <form action="profile/controller/Sign_in_controller.jsp" method="post">
    Enter User Name
    <input type="text" name="user_name"> <br>
    Enter Password
    <input type="password" name="password"><br>
    <input type="submit" value="Submit">
    </form>
    <%
    String message=(String)session.getAttribute("login_message");
    if(message!=null){
    out.println(message);
    session.removeAttribute("login_message");
    }
    %>
    </center>
    </body>
    </html>
  10. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <display-name>Login_Project</display-name>
     <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>default.html</welcome-file>
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <servlet> 
     <servlet-name>Home</servlet-name>
     <jsp-file>/profile/view/Home.jsp</jsp-file>
     </servlet>
     <servlet-mapping>
     <servlet-name>Home</servlet-name>
     <url-pattern>/user-home-page/*</url-pattern> 
     </servlet-mapping>
     <servlet> 
     <servlet-name>My_Profile</servlet-name>
     <jsp-file>/profile/view/My_Profile.jsp</jsp-file>
     </servlet>
     <servlet-mapping>
     <servlet-name>My_Profile</servlet-name>
     <url-pattern>/user-profile</url-pattern> 
     </servlet-mapping>
     <servlet> 
     <servlet-name>Signoutcontroller</servlet-name>
     <jsp-file>/profile/controller/Signoutcontroller.jsp</jsp-file>
     </servlet>
     <servlet-mapping>
     <servlet-name>Signoutcontroller</servlet-name>
     <url-pattern>/Signoutcontroller</url-pattern> 
     </servlet-mapping>
     </web-app>
  11. http://localhost:8080/Login_Project/index.jsp
    
    
  12. http://localhost:8080/Login_Project/user-home-page/jinu1234
    
    
  13. http://localhost:8080/Login_Project/user-profile
    
    
  14. After Logout

Download the complete project here

Login_Project