JAVA LOGIN MODULE USING MVC

Login Page:

Login.html:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body bgcolor="lightblue">
        <br><br>  <br><br>

  <!--(comment line ) download the jquery plugin 2 lines -->
        <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
        <script>


            jQuery(document).ready(function($) {
                $("#login").validate({
                    ignore: [],
                    rules: {
                        name: {
                            required: true
                        },
                        pw: {
                            required: true,
                            minlength: 6
                        },
                    },
                    messages: {
                        name: {
                            required: "Please enter the name."
                        },
                        pw: {
                            required: "Please enter the password.",
                            minlength: "Please enter the password greater than 6.",
                        },
                    }
                });
            });



        </script>
        <style>
            .error{
                color:red;
            }
            .button{
                padding: 5px;
                width: 6%;
                background: cornflowerblue;
                color: white;
            }
        </style>

    <center>


        <form method="POST" id="login" action="login">
            <font color="blue" size="4">
            <h2> Login module </h2>
            </font>
            <div>
                <table>
                    <tr>
                        <td> Email ID: </td>
                        <td> <input type="text" name="email"/></td>
                    </tr>
                    <br><br>
                    <tr>
                        <td> Password: </td>
                        <td> <input type="password" name="pw"/></td>
                    </tr>
                </table>
                <div style="text-align: center;margin-left: 70px;margin-top: 10px;">
                    <input type="submit" class="button" value="login"/>
                </div>
            </div>
        </form>

    </center>


</body>
</html>

Package name:handler

Servlet name: login

package handler;

import DAO.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "login", urlPatterns = {"/login"})
public class login extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       HttpSession session = request.getSession();
        try (PrintWriter out = response.getWriter()) {

            String email = request.getParameter("email");
            String pass = request.getParameter("pw");
          
           

            user u = new user(session);
            String status = u.login(email, pass);

            if (status.equals("success")) {
              //The RequestDispatcher interface provides the facility of 
                //dispatching the request to another resource it may be html, servlet or jsp.
                RequestDispatcher rd1 = request.getRequestDispatcher("success.jsp");

                //The getRequestDispatcher() method of ServletRequest interface returns the object //of RequestDispatcher
                rd1.forward(request, response);

                 //   Forwards a request from a servlet to another resource (servlet, JSP file, or //HTML file) on the server.
            } else if (status.equals("failure")) {
              

                RequestDispatcher rd1 = request.getRequestDispatcher("failure.jsp");

                rd1.forward(request, response);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    

}

Package name:DAO

Class name:user.java

package DAO;

import java.sql.*;
import javax.servlet.http.HttpSession;

public class user 
{

    Connection con;
    Statement st;
    ResultSet rs = null;
    String status = "";
    HttpSession se;
    
    student s;

    public user(HttpSession session) {
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sookshmas", "root", "root");
 se = session;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public String login(String email,String pass)
{
   try
   {
       st=con.createStatement();
       rs=st.executeQuery("select * from sookshmas where email='"+email+"' and password='"+ pass+"' ");
       boolean b=rs.next();
       if(b==true)
       {
 name = rs.getString("name");
                se.setAttribute("uname", name);
//public void setAttribute(String name, Object value): Binds the object with a name and stores the //name/value pair as an attribute of the HttpSession object.
           status="success";
       }
       else
       {
           status="failure";
       }
   }
   catch(Exception e)
   {
       e.printStackTrace();
   }
    
    return status;
}
}

Success.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         
  <%-- comment lines  public Object getAttribute(String name): Returns the String object specified in the parameter, from the session object. --%>

  <%
 String name = (String)request.getSession().getAttribute("uname");
out.println("<html>");
out.println("<body bgcolor='lightgreen'>");
out.println("<center><br><br>");

out.print("<h1>Hello "+name +"</h1>");  
  
%> 
<%-- comment lines --%>
<%-- or(  we can write the below code also 
  <%if (session.getAttribute("uname") != null) {%>  
        <h1>Hello <%= session.getAttribute("uname")%></h1> 
        <%}%>
    </body>
</html>
--%>

</body>
</html>

failure.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <%   
  

out.println("<html>");
out.println("<body bgcolor='lightgreen'>");
out.println("<center><br><br>");

out.print("<h1>Hello login failed </h1> ");  
  
%>  
    </body>
</html>

Posted on by