Wednesday, 20 December 2017

Login And Registration Form in JSP With MySQL Database

Step 1:   
 Create Database LearningPoint92;

Step 2:
   Use LearningPoint92;

Step 3:
Create table Emp (
id int(20) unsigned NOT NULL auto_increment,
First_name varchar(50) NOT NULL,
Last_name varchar(50) NOT NULL,
Email varchar(50) NOT NULL,
Username varchar(50) NOT NULL,
Password varchar(50) NOT NULL,
Joiningdate date NOT NULL,
PRIMARY KEY  (id));

Step 4:

Create New Project (WebApplication) and Add JSP Page
Add Jar file (MySQL-connector) in Libraries

Step  5:
Index.jsp
<%--
    Document   : index
    Created on : Dec 20, 2017, 2:39:29 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>index Page</title>
    </head>
    <body>
        <form method="post" action="login.jsp">
            <center>
            <table border="2" width="35%" cellpadding="2">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname"/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass"/></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Login" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">Yet Not Registered!! <a href="DesignReg.jsp">Register Here</a></td>
                    </tr>
                </tbody>
            </table>
            </center>
        </form>
 </body>
</html>
Step 6:
DesignReg.jsp
<%--
    Document   : DesignReg
    Created on : Dec 20, 2017, 2:43:01 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>DesignReg</title>
    </head>
    <body>
       <form method="post" action="Codereg.jsp">
            <center>
            <table border="2" width="35%" cellpadding="5">
                <thead>
                    <tr>
                        <th colspan="2">Enter Information Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname"  /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname"  /></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><input type="text" name="email"  /></td>
                    </tr>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass"  /></td>
                    </tr>
                    <tr>
                        <td>JoiningDate</td>
                        <td><input type="date" name="d1"  /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
                    </tr>
                </tbody>
            </table>
            </center>
        </form>
 </body>
</html>
Step 7:
Codereg.jsp
<%--
    Document   : Codereg
    Created on : Dec 20, 2017, 2:45:22 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import ="java.sql.*" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Codereg Page</title>
    </head>
    <body>
      
<%
    String user = request.getParameter("uname");   
    String pwd = request.getParameter("pass");
    String fname = request.getParameter("fname");
    String lname = request.getParameter("lname");
    String email = request.getParameter("email");
    String jdate = request.getParameter("d1");

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LearningPoint92",  "root", "admin");  
// LearningPoint92 is database name, root is username, admin is password

    Statement st = con.createStatement();
    int i = st.executeUpdate("insert into Emp(First_name, Last_name, Email, Username, Password, Joiningdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "','" + jdate +"')");
    if (i > 0) {
      response.sendRedirect("Welcome.jsp");
        }
    else
{
        response.sendRedirect("index.jsp");
    }
%>
</body>
</html>
Step 8:
Welcome.jsp
<%--
    Document   : Welcome
    Created on : Dec 20, 2017, 2:47:34 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome Page</title>
    </head>
    <body>
     Registration is Successful.
 Please Login Here <a href='index.jsp'>Go to Login</a>
    </body>
</html>
Step 9:
Login.jsp
<%--
    Document   : login
    Created on : Dec 20, 2017, 2:48:37 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>login Page</title>
    </head>
    <body>
       
<%
    String userid = request.getParameter("uname");   
    String pwd = request.getParameter("pass");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LearningPoint92","root", "admin");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from Emp where Username='" + userid + "' and Password='" + pwd + "'");
    if (rs.next()) {
        session.setAttribute("userid", userid);
        //out.println("welcome " + userid);
        //out.println("<a href='logout.jsp'>Log out</a>");
        response.sendRedirect("Result.jsp");
    }
    else {
        out.println("Invalid password <a href='index.jsp'>try again</a>");
    }
%>
</body>
</html>
Step 10:
Result.jsp
<%--
    Document   : Result
    Created on : Dec 20, 2017, 2:52:10 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Result Page</title>
    </head>
    <body>
      <%
    if ((session.getAttribute("userid") == null) || (session.getAttribute("userid") == "")) {
%>
You are not logged in<br/>
<a href="index.jsp">Please Login</a>

<%}
else {
%>
Welcome <%=session.getAttribute("userid")%>
<a href='logout.jsp'>Log out</a>
<%
    }

%>
</body>
</html>
Step 11:
logout.jsp
<%--
    Document   : logout
    Created on : Dec 20, 2017, 2:53:18 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>logout Page</title>
    </head>
    <body>
        <%
session.setAttribute("userid", null);
session.invalidate();
response.sendRedirect("index.jsp");
%>
</body>
</html>

Step 12:

Output:
Login Here
User Name
Password
Yet Not Registered!! Register Here

Enter Information Here
First Name
Last Name
Email
User Name
Password
JoiningDate
Already registered!! Login Here

Step 13:
Check in Mysql database Whether data is inserted or not

Select * from Emp;

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...