Create a simple authentication system in Eclipse

Home > Search

This assumes you have created your first dynamic web project in Eclipse and you can can connect to a database.

Create the login.jsp page that will contain the form where the username and password can be provided.

<form action="/beta/Login" method="post">
  Username : <input type="text" name="username"><br />
  Password : <input type="password" name="password"><br />
  <input type="submit">



If you have not yet created a package, in the left panel of Eclipse, expand the Java Resources folder, right click on the src folder, and select New > Package. Give the package a name, such as com.main.servlet and select Finish. Right click on the package and select New > Servlet. Give the servlet a name, such as Login, and select Finish.

Import the following items.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


In the doPostmethod section of the markup, add the following.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
        } catch (ClassNotFoundException e) {

        String user = request.getParameter("username");
        String pass = request.getParameter("password");

        String sql = "select * from authentication where username='" + user + "'";

        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://,"db_username","db_password");
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            String un = null;
            String pw = null;
            PrintWriter prwr1 = response.getWriter();               
                prwr1.write("<p> No Such User in Database</p>");

            while ( {
                un = rs.getString("username");
                pw = rs.getString("password");
            PrintWriter pww = response.getWriter();

            if (un.equalsIgnoreCase(user) && pw.equals(pass)) {
                pww.write("<p>Welcome, " + user + "</p>");
            } else {
                pww.write("<p>The username or password you provided are invalid</p>\n");
        } catch (SQLException e) {




Add the following to your web.xml file.



Run the application in Eclipse, or export the WAR and run the application on an application server. When navigating to index.jsp and providing a valid username and password, text Welcome followed by the provied username should be displayed.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.