Site icon DataFlair

How to Create Registration Page in Same Login Form in JSP

Program 1

<%@page  import="java.sql.*"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post" action="login.jsp">
          <center>
              <table border="1">
                  <tr>
                      <th>User Name</th>
                      <td><input type="text" name="txtuser"></td>
                  </tr>   
                  <tr>
                      <th>Password</th>
                      <td><input type="password" name="txtpass"></td>
                  </tr>   
                  <tr>
                      <td></td>
                      <td>
                          <input type="submit" value="Submit">
                          <input type="reset" value="Reset">
                      </td>                  
                   </tr> 
                  <tr> 
                      <th><A href="empreg.jsp">New user click here</A></th> 
                  </tr> 
              </table> 
          <% 
              
           
            String user=null,pass=null;
            user=request.getParameter("txtuser");
            pass=request.getParameter("txtpass");
          if(user!=null && pass!=null)  
          {     
               Connection con=null;
               PreparedStatement ps=null;
               ResultSet rs=null;
                 // Driver load
                try
                {
                    Class.forName("com.mysql.jdbc.Driver");
                    //out.println("Driver load succefully......");
                }
                catch(ClassNotFoundException e)
                {
                     out.println(e);
                }
                //Database Connection code
                try
                {
                   con=DriverManager.getConnection("jdbc:mysql://localhost/dataflair", "root", "root@data");
                    //out.println("Database Connected......");
                   String sql;
                  sql="select * from emplogin where userid=? and password=?";
                  ps=con.prepareStatement(sql);
                  ps.setString(1, user);
                  ps.setString(2, pass);
                 rs=ps.executeQuery();
                 if(rs.next())
                    out.println("<font color=green size=5>Valid User</font>");
                     //response.sendRedirect("user.html");
                 else
                    out.println("<font color=red size=5>Invalid user id or password</font>");
                }
                 catch(SQLException e)
                {
                     out.println(e);
                }
                
          }   
              
              
           
              
          %>    
          </center>
        </form>    
    </body>
</html>

Program 2

<%@page  import="java.sql.*"%>
<html>
<head><title>Employee Page</title></head>
<body>
<center>
<h2><font color=green>Employee Registration Section</font></h2>
<form method=post>
<table border=1>
<tr>
<th>User Name:</th>
<td><input type=text name=txtusername></td>
</tr>
<tr>
<th>Password:</th>
<td><input type=password name=txtpassword></td>
</tr>
<tr><td></td>
<td>
<input type=submit value=submit>
<input type=reset value=reset>
</td>
</tr>
</table>
</form>
<
<% 
       String username=null,password=null;
       Connection con=null;
       PreparedStatement ps=null;
       username=request.getParameter("txtusername");
       password=request.getParameter("txtpassword");
       if(username!=null && password!=null)
       {
             //Driver load
            try
            { 
                Class.forName("com.mysql.jdbc.Driver");
                //out.println("Driver load........");
            }
            catch(ClassNotFoundException e)
            {
                out.println(e);
            }
            //Data base connection
            try
            {
               con=DriverManager.getConnection("jdbc:mysql://localhost/dataflair", "root", "root@data");
               // out.println("Database connection success........");
              // Query Execution
               String sql;
               sql="insert into emplogin values(?,?)";
               ps=con.prepareStatement(sql);
               ps.setString(1, username);
               ps.setString(2, password);
              if(ps.executeUpdate()>0)
                  out.println("Registration done succefully......");
            }
            catch(SQLException e)
            {
                 out.println(e);  
            }
       }
%>
</center>
</body>
Exit mobile version