Coding a Servlet with JDBC in Java

Create a Login Servlet that validates the username and password using JDBC connectivity with a MySQL Database


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome to our Login Page</title>
</head>
<body>
<form action="login" method="post">
<label for="usernamelbl">Enter your Username</label>
<input type="text" name="username" required><br>
<label for="passlbl">Enter your Password</label>
<input type="password" name="password" required><br>
<input type="submit" name="submit" value="submit">
<input type="reset">
</form>
</body>
</html>

Login Servlet Code

import java.io.IOException;
import java.io.PrintWriter;
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;
import java.sql.*;
/**
  * Servlet implementation class LoginServlet
  */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
        
    /**
      * @see HttpServlet#HttpServlet()
      */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
    * @see Servlet#init(ServletConfig)
    */
  public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
  }

  /**
    * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
    */
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String method= request.getMethod();
    if(method.equals("GET"))
    {
      doGet(request, response);
    }
    else if(method.equals("POST"))
    {
      doPost(request, response);
    }
  }

  /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    try
    {
      student s = new student(username,password);
      boolean result=s.validateStudent();
      if(result)
      {
        out.print("Welcome");
      }
      else
      {
        out.println("Enter Valid Data");
      }
    }
    catch(Exception e)
    {
      System.out.println(e.getLocalizedMessage());
    }
  }
}

class student
{
  private String username;
  private String password;

  public student(String username, String password) {
    this.username = username;
    this.password = password;
  }

  public boolean validateStudent() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
    dbmsConnection connect=null;
    PreparedStatement stmt=null;
    Connection con=null;
    boolean found=false;
    try
    {
    connect = new dbmsConnection("jdbc:mysql://localhost:3306/vit","root","");
    con = connect.getConnection();
    String sql = "select * from student where name=? and password= ?;";
    stmt = con.prepareStatement(sql);
    stmt.setString(1, username);
    stmt.setString(2, password);
    ResultSet rs = stmt.executeQuery();
    if(rs.next()==false)
    {
      found=false;
    }
    else
    {
      found=true;
    }
    }catch(Exception e)
    {
      System.out.println(e.getLocalizedMessage());
    }
    finally {
      connect.closeConnection(con, stmt);
    }
    
    return found;
    
  }
}

class dbmsConnection
{
  String url;
  String username;
  String password;

  public dbmsConnection(String url, String username, String password) {
    this.url = url;
    this.username = username;
    this.password = password;
  }

  public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
    Connection con=null;
    try
    {
      Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
      con = DriverManager.getConnection(url,username,password);
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());
    }
    return con;
  }
  
  public void closeConnection(Connection con,Statement stmt) throws SQLException {
    stmt.close();
    con.close();
  }
  
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>firstservletproject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>firstservlet</servlet-name>
  <servlet-class>LoginServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>firstservlet</servlet-name>
  <url-pattern>/login</url-pattern>
  
  </servlet-mapping>
  </web-app>