Login Form in Servlet

Login Form in Servlet

In this example, we are going to create the Login Form using Servlet. Please read our previous article where we discussed how to write data into PDF file using servlet. For this Login Form using Servlet example, you just need to create a database loginDB and table login with the following three fields: name, email, and password. The database looks like this:

Login Form in Servlet

Create a table “login” in your database as shown below:

CREATE TABLE LOGIN
(
     Name VARCHAR2(50),
     Email VARCHAR2(100),
     Password VARCHAR2(45)
)

Then insert some data into the table:

Login Form using Servlet Based Java Web Application

In this example, we have created three pages:

  1. Login.html
  2. LoginServlet.java
  3. Welcome.java

In Login.html, we have to get input from the user using text fields and a Combobox. The information entered by the user is forwarded to LoginServlet.java servlet which is responsible to validate the data from the database. LoginServlet.java servlet receives all the data entered by the user and validates it from the database. If the entered data are correct, it will redirect you to the Welcome.java page where you will get the “logout” button. If you want to logout from the web page, click on the “logout” button which will redirect back you to Login.html.

Login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
</head>
<body>
 <form action="LoginServlet" method="post">
  Email ID:<input type="text" name="email" /><br />
  <br /> Password:<input type="password" name="password" /><br />
  <br /> <input type="submit" value="login" formaction="LoginServlet">
 </form>
</body>
</html>
LoginServlet.java
package com.servlet;
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletContext;
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.*;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  // ServletContext sc = getServletContext();
  HttpSession session = request.getSession();
  String email = request.getParameter("email");

  session.setAttribute("email", email);
  // String email1=sc.getInitParameter("email");
  // sc.setAttribute("email",email);
  String password = request.getParameter("password");
  // HttpSession session=request.getSession();

  try {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/loginDB", "root", "root");
   PreparedStatement pst = conn
     .prepareStatement("Select email,password from login where email=? and password=?");
   pst.setString(1, email);
   pst.setString(2, password);
   ResultSet rs = pst.executeQuery();
   if (rs.next()) {
    out.print("You are successfully loggedin...");
    request.getRequestDispatcher("Welcome").include(request, response);
   } else {

    out.println("Username or Password incorrect");
    request.getRequestDispatcher("Login.html").include(request, response);

   }
  } catch (ClassNotFoundException | SQLException e) {
   e.printStackTrace();
  }
 }
}
Welcome.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  PrintWriter out = response.getWriter();
  out.println("<form action='LoginServlet' method='post'>");
        out.println("</br>Welcome user");
        out.println("<input type='submit' value='logout' formaction='Login.html'>");
        out.println("</form>");
 }
}

Output

Run your code to get the following output. Enter your email id and password to log in.

Login Form in Java Servlet Based Web Application

If your entered data is correct, you will get the following output. Click on the “logout” button to logout which will redirect you back to the Login page.

Login Form in Java Servlet

If your entered data is incorrect, you will get the following output:

how to develop Login Form in Servlet

In the next article, I am going to discuss how to display images using Servlet. Here, in this article, we develop Login Form in Servlet and I hope you enjoy how to develop Login Form in Servlet article.

Leave a Reply

Your email address will not be published.