Wednesday, February 7, 2018

Login form using JSP and MySQL

In this session you will learn how to develop a login form using jsp and MySQL database connection.MySQL has always been my favorite database because it’s very easy to use.You can use any IDE like Netbeans or Eclipse to build a JSP application. I prefer Netbeans over Eclipse.e building the application, make sure you have the MySQL Java Connector added to the libraries. Without it, the server (for e.g. Glass Fish Server) will throw an error.

Step 1: Create Login page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Demo with JSP</title>
    </head>
    <body>
        <form method="post" action="validate.jsp">
            <center>
            <table border="1" cellpadding="5" cellspacing="2">
                <thead>
                    <tr>
                        <th colspan="2">Login</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Username</td>
                        <td><input type="text" name="username" required/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="password" required/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Login" />
                            &nbsp;&nbsp;
                            <input type="reset" value="Reset" />
                        </td>                       
                    </tr>                   
                </tbody>
            </table>
            </center>
        </form>
    </body>
</html>

Step 2: Now create the validation page

After creating login page,this page will validate the username and password against the MySQL database.

<%@ page import ="java.sql.*" %>
<%
    try{
        String username = request.getParameter("username");  
        String password = request.getParameter("password");
        Class.forName("com.mysql.jdbc.Driver");  // MySQL database connection
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javademo?" + "user=root&password=");   
        PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=? and pass=?");
        pst.setString(1, username);
        pst.setString(2, password);
        ResultSet rs = pst.executeQuery();                       
        if(rs.next())          
           out.println("Valid login credentials");       
        else
           out.println("Invalid login credentials");           
   }
   catch(Exception e){      
       out.println("Something went wrong !! Please try again");      
   }     
%>

Once you have finished both your login and validate page, you can run the application in the browser. 

Output:
 

Note:
You must add JAR files when you use MySql database. In this case make sure you have added the MySQL Java Connector JAR file.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...