Wednesday, 8 October 2014

Using Basic JDBC in Servlets - Creating a Login/SignUp page



This program helps you to understand the basics of JDBC and applying the same in Servlets, by creating a login/signUp page. This program verifies the username and password from the database if the user login, otherwise if the user signUp, then it inserts the corresponding entries into the database.


Login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
      <form action="SignInCls">
            <table align="center">
                  <tr>
                        <td>Login</td>
                        <td><input type="text" name="username"></td>
                  </tr>
                  <tr>
                        <td>Password</td>
                        <td><input type="password" name="password"></td>
                  </tr>
                  <tr>
                        <td><input type="submit" name="signin" value="Sign In"></td>
                  </tr>
            </table>
            <br>
            <br>
            <h5 align="center">Dont have an Account... Create it...</h5>
            <br>
            <table align="center">
                  <tr>
                        <td>UserName</td>
                        <td><input type="text" name="username1"></td>
                  </tr>
                  <tr>
                        <td>Password</td>
                        <td><input type="password" name="password1"></td>
                  </tr>
                  <tr>
                        <td>First Name</td>
                        <td><input type="text" name="firstname"></td>
                  </tr>
                  <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lastname"></td>
                  </tr>
                 
                  <tr>
                        <td><input type="submit" name="signup" value="Sign Up"></td>
                  </tr>
            </table>
           
      </form>
</body>
</html>

/************************************************************************/
SignInCls.java
package SignInPkg;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SignInCls
 */
@WebServlet("/SignInCls")
public class SignInCls extends HttpServlet {
      Connection cn;
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignInCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see Servlet#init(ServletConfig)
       */
      public void init(ServletConfig config) throws ServletException
      {    
            try
            {
                  Class.forName("com.mysql.jdbc.Driver");
                  try {
                        cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","lavikothari");
                  } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                 
            } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            PrintWriter pw=response.getWriter();
            try
            {
                       
                  if(request.getParameter("signin")!=null)
                  {
                        PreparedStatement pst=cn.prepareStatement("select*from login where username=? and password=?");
                        pst.setString(1, request.getParameter("username"));
                        pst.setString(2, request.getParameter("password"));
                        ResultSet rs=pst.executeQuery();
                        if(rs.next())
                        {
                              pw.println("Login Successful");
                        }
                        else
                        {
                              pw.println("Login not Successful");
                        }
                  }
                  if(request.getParameter("signup")!=null)
                  {
                        PreparedStatement pst=cn.prepareStatement("insert into login values(?,?,?,?);");
                        pst.setString(1, request.getParameter("username1"));
                        pst.setString(2, request.getParameter("password1"));
                        pst.setString(3, request.getParameter("firstname"));
                        pst.setString(4, request.getParameter("lastname"));
                        pst.executeUpdate();
                        pw.println("Account Created Successfully");
                  }
            }
            catch(SQLException e)
            {
                 
            }
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

No comments:

Post a Comment