Thursday 1 September 2016

Registration AND LOGIN form in JSP 




STEPS WRITTEN BY ME ITSELF FOR PRACTICAL  FOR EASY UNDERSTANDING OF JSP(JAVA SERVER PAGE)

1) install tomcat 6.0

2) install sql server 10g

3)make database (product) in sql server and create table MEMBERS
first_name, last_name, email, uname, pass, regdate

4) insert records in MEMEBRS table

5) set environment variables in my computer

variable name=>classpath
variable value=>C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.JAR;.;

6)set environment variables in my computer for tomcat

variable name=>JAVA_HOME
variable_value=>C:\Program Files (x86)\Java\jdk1.6.0_18

7)create folder in tomcat6.0 =>webapps=>JSPREG=>WEB-INF =>classes , lib and reg.xml

8)paste ojdbc.jar file in lib 

9) CREATE THE FILE REG.JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration</title>
    </head>
    <body>
        <form method="post" action="registration.jsp">
            <center>
            <table border="1" width="30%" 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" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass" value="" /></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>


10) CREATE <%@ page import ="java.sql.*" %>
<%
    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");

try
{
    Class.forName("oracle.jdbc.driver.OracleDriver");
   
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","vision");
    Statement st = con.createStatement();
    //ResultSet rs;
    int i = st.executeUpdate("insert into members(first_name, last_name, email, uname, pass, regdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "', CURRENT_DATE)");
    if (i > 0) {
        //session.setAttribute("userid", user);
        response.sendRedirect("welcome.jsp");
       // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
    } else {
        response.sendRedirect("index.jsp");
    }
}
catch(Exception e)
{
out.println(e);
}
%>


11) CREATE SUCCESS.JSP

<%
    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>
<%
    }

%>


12)CREATE WELCOME.JSP

Registration is Successful.

Please Login Here <a href='index.jsp'>Go to Login</a>


13)CREATE INDEX FILE=>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Example</title>
    </head>
    <body>
        <form method="post" action="login.jsp">
            <center>
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass" value="" /></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="Reg.jsp">Register Here</a></td>
                    </tr>
                </tbody>
            </table>
            </center>
        </form>
    </body>

</html>


14)CREATE LOGIN.JSP

<%@ page import ="java.sql.*" %>
<%
    String userid = request.getParameter("uname");    
    String pwd = request.getParameter("pass");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","vision");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from members where uname='" + userid + "' and pass='" + pwd + "'");
    if (rs.next()) {
        session.setAttribute("userid", userid);
        //out.println("welcome " + userid);
        //out.println("<a href='logout.jsp'>Log out</a>");
        response.sendRedirect("success.jsp");
    } else {
        out.println("Invalid password <a href='index.jsp'>try again</a>");
    }

%>



15)CREATE LOGOUT.JSP

<%
session.setAttribute("userid", null);
session.invalidate();
response.sendRedirect("index.jsp");

%>




 TYPE THE 

===>      Localhost:9999/jspreg/


THE LOGIN PAGE WILL OPEN



THE REGISTRATION PAGE












In this example you will see how to develop a registration AND
 LOGIN form in JSP. To develop a registration form you will need to connect your servlet application with database. Here we are using SQL SERVER 10 G database.

No comments:

Post a Comment