Login page using jsp and servlet with mysql database

        This login example was developed using Servlet, Jsp and mysql database connectivity

Environment used

    Servlet 3.1 jar

    Mysql 5.1 server or greater version and its mysql connector jar

    Eclipse luna EE or greater version

    tomcat 8

Login page (index.html)

<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

Simple login Example using servlet jsp and mysql database connectivity
<br> Create a test database, student table and insert some user
information in it.

<form action="LoginController" method="post">
Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">


web.xml mapping

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">


Servlet code for login page with database mysql

package com.candidjava;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

* Servlet implementation class LoginController
public class LoginController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String un=request.getParameter("username");
String pw=request.getParameter("password");

// Connect to mysql and verify username password

try {
// loads driver
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); // gets a new connection

PreparedStatement ps = c.prepareStatement("select userName,pass from student where userName=? and pass=?");
ps.setString(1, un);
ps.setString(2, pw);

ResultSet rs = ps.executeQuery();

while (rs.next()) {
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block


Success Page

<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
Login success

error Page

<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
Invalid username or password, Please try again with valid

Output screenshot


        Servlet Jsp Login example with mysql war

        Servlet Jsp Login example with mysql eclipse zip

Tech Tip : Migrate your SQL server into the cloud to remotely monitor/access it from anywhere, anytime on any device(PC/Mac/android/iOS) on high performance hosted citrix xendesktop from CloudDesktopOnline with cheapest xendesktop cost. Learn more about hosted applications on MS azure and managed azure services by visiting one of the best cloud hosting providers - Apps4Rent.com.

Related Post


         Hi, I tried the same code and I tried running the code but after compilation it got failed with "HTTP Status 405 ? Method Not Allowed" . could you please help on this.
         Confused with GET and POST request, Check your html and Servlet file for method type mismatch
         how to form login and logout timing form
         I am truly grateful to the owner of this web page who has shared this impressive post at at this time.
         i need one help on this issue, could you please help me here. i am trying to run this above code but i am hitting with a issue as below WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:LoginPage' did not find a matching property. thanks in advance,
         Looks like you had some problem with eclipse workspace
         Hi, I tried the same code and I tried running the code but after compilation it got failed with "HTTP Status 405 ? Method Not Allowed" . could you please help on this.
         Today anyone has an Android device all across the world. This has lead to developing a tons of applications that provide servises such as games, entertainment playgrounds and many others. To download any of these applications a user must get an account at Google Play. But not all applications are available there as they don't meet the Android rules. To combat this a new app was developed - TutuApp. This helper works on Android run smartphones and tablets and it has the biggest store of apps and games. We name a few: Spotify, Pokemon, Clash of Clans and others. They come with zero ads and locked content! Whant to know more? Visit Download TutuApp APK to download TutuApp for free on your Android phone. Installation instructions for TutuApp are available at that page.