0% found this document useful (0 votes)
101 views2 pages

JSP Login Form + JDBC + MySQL Example

Uploaded by

yaseenbaig2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views2 pages

JSP Login Form + JDBC + MySQL Example

Uploaded by

yaseenbaig2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Guides

Check out my 10+ Udemy bestseller courses and


discount coupons: Udemy Courses - Ramesh
Fadatare

Java ! JavaEE ! Library ! REST !

Spring Boot ! Microservices ! Full Stack !

! YouTube UI ! Quiz ! Hibernate ! DB !

Programs ! Kotlin ! Python ! Me !

Viksit Bharat Sankalp


Yatra

Viksit Bharat Sankalp Yatra

Viksit Bharat

Open

JSP Login Form +


JDBC + MySQL
Example
author: Ramesh Fadatare

JDBC 4.2 JSP TUTORIAL MYSQL

< Previous Next >


JSP Tutorial

This article is a series of JSP Tutorial. In this

article, we will build a simple Login Form

using JSP, JDBC and MySQL database.

In this example, we will write JDBC code

separate from the JSP page. JSP page we

will be using only for presentation.

Let me list out the tools and technologies

that I have used to develop this application.

You can download the source code of this

article from my GitHub repository. The link

has given at end of this article.

In this article, we will design below the Login

Form JSP page and store Form parameters

into the MySQL database:

Tools and technologies used

JSP - 2.2 +

IDE - STS/Eclipse Neon.3

JDK - 1.8 or later

Apache Tomcat - 8.5

JSTL - 1.2.1

MySQL - mysql-connector-java-

8.0.13.jar

Development Steps

1. Create Eclipse Dynamic web project

2. Add Dependencies

3. Project Structure

4. MySQL Database Setup

5. Create a JavaBean - Login.java

6. Create a LoginDao.java

7. Create a login.jsp

8. Create a loginsuccess.jsp

9. Demo

1. Create an Eclipse Dynamic


Web Project

To create a new dynamic Web project in

Eclipse:

1. On the main menu select File > New >

Project....

2. In the upcoming wizard choose Web >

Dynamic Web Project.

3. Click Next.

4. Enter project name as "login-jsp-jdbc-

example";

5. Make sure that the target runtime is set to

Apache Tomcat with the currently supported

version.

2. Add Dependencies

Add the latest release of below jar files to the

lib folder.

- jsp-api.2.3.1.jar
- servlet-api.2.3.jar
- mysql-connector-java-8.0.13.jar

In Eclipse, paste these JAR files to your

project directory: WebContent/WEB-INF/lib

3. Project Structure

Standard project structure for your reference

4. MySQL Database Setup

Let's create a database named

"mysql_database" in MySQL. Now, create a

login table using below DDL script:

CREATE TABLE `login` (


`username` varchar(45) NOT NULL,
`password` varchar(45) DEFAULT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

Here is an Insert SQL statement:

INSERT INTO `mysql_database`.`login` (

5. Create a JavaBean -
LoginBean.java

Let's create a LoginBean class which we

will use in JSP action tags.

package net.javaguides.login.bean;

import java.io.Serializable;

public class LoginBean implements Serial


/**
*
*/
private static final long serialVers
private String username;
private String password;

public String getUsername() {


return username;
}

public void setUsername(String usern


this.username = username;
}

public String getPassword() {


return password;
}

public void setPassword(String passw


this.password = password;
}
}

6. Create a LoginDao.java

We will separate JDBC accessing code

separate from JSP. So let's create an

LoginDao.java class under

net.javaguides.jsp.jdbc.database

package and add following code to it:

package net.javaguides.login.database

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

import net.javaguides.login.bean.LoginBe

public class LoginDao {

public boolean validate(LoginBean


boolean status = false;

Class.forName("com.mysql.jdbc.Dr

try (Connection connection =


.getConnection("jdbc:mysql:/

// Step 2:Create a statement


PreparedStatement preparedSt
.prepareStatement("select *
preparedStatement.setString(
preparedStatement.setString(

System.out.println(preparedS
ResultSet rs = preparedState
status = rs.next();

} catch (SQLException e) {
// process sql exception
printSQLException(e);
}
return status;
}

private void printSQLException(SQLEx


for (Throwable e: ex) {
if (e instanceof SQLExceptio
e.printStackTrace(System
System.err.println("SQLS
System.err.println("Erro
System.err.println("Mess
Throwable t = ex.getCaus
while (t != null) {
System.out.println(
t = t.getCause();
}
}
}
}
}

7. Create a login.jsp

Let's design login HTML form with following

fields:

username

password

<%@ page language="java" contentType=


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title
</head>

<body>
<h1>Employee Login Form</h1>
<form action="loginsuccess.jsp
<table style="with: 100%"
<tr>
<td>UserName</td>
<td><input type="
</tr>
<tr>
<td>Password</td>
<td><input type="
</tr>

</table>
<input type="submit" value
</form>
</body>

</html>

8. Create a loginsuccess.jsp

After an employee successfully login then

this page show a successful message on

screen:

<%@ page language="java" contentType=


pageEncoding="ISO-8859-1"%>
<%@page import="net.javaguides.login
<!DOCTYPE html>
<html>

<head>
<meta charset="ISO-8859-1
<title>Insert title here</
</head>

<body>

<jsp:useBean id="login" clas

<jsp:setProperty property

<%
LoginDao loginDao = new LoginDao();
boolean status = loginDao.validate(lo
if (status) {
out.print("<h1>You have login
}
%>
</body>

</html>

Note that in the above page, we have used

JSP action tags. Read more about action

tags here.

9. Demo

It's time to see a demo of the above

development. Deploy this web application in

tomcat server.

Employee Login Form

Once you deploy this application

successfully then hit this link into a browser

- http://localhost:8080/login-jsp-jdbc-

mysql-example/login.jsp

Login Success Page

GitHub Repository

https://github.com/RameshMF/JSP-

Tutorial

In the next article, we will learn how to

develop a custom JSP tag with an

example.

< Previous Next >


JSP Tutorial

JDBC 4.2 JSP TUTORIAL MYSQL

To leave a comment, click the button


below to sign in with Google.

SIGN IN WITH GOOGLE

Subscriber to my top YouTube Channel


(130K+ Subscribers)

Java Guides

YouTube 140K

Dev Tools / Utilities

Spring Boot Code Generator


Java JDBC Code Generator
Online JWT Generator
Online JWT Decoder
Online JSON Viewer
Online JSON Validator
Online HTML Formatter
Base64 Encode Online
Base64 Decode Online
URL Encoder Online
URL Decoder Online
Online SQL Formatter
Online HTML Compiler
Online CSS Formatter
Online JSON Parser
HTML Escape / Unescape Online
XML Escape / Unescape Online
SQL Escape / Unescape Online
JSON Escape / Unescape Online
JavaScript Escape / Unescape Online
Java Escape / Unescape Online
Properties to YAML Converter Online

My Udemy Course: Building Microservices


with Spring Boot and Spring Cloud

My Udemy Course - Building Real-Time REST


APIs with Spring Boot

My Udemy Course - Testing Spring Boot


Application with JUnit and Mockito

My Udemy Course - Master Spring Data JPA


with Hibernate

Spring Boot Thymeleaf Real-Time Web


Application Course

My Udemy Course - Spring Boot RabbitMQ


Course - Event-Driven Microservices

My Udemy Course - Spring Boot + Apache


Kafka Course

About Me

Hi, I am Ramesh Fadatare. I am


VMWare Certified Professional for
Spring and Spring Boot 2022.

I am the founder and author of this


blog website JavaGuides, a
technical blog dedicated to the
Java/Java EE technologies and Full-Stack Java
development.

All the articles, guides, and tutorials(2.5K +)


written by me so connect with me if you have
any questions/queries. Read more about me at
About Me.

Top YouTube Channel (125K+ Subscribers):


Check out my YouTube channel for free videos
and courses - Java Guides YouTube Channel

My Udemy Courses -
https://www.udemy.com/user/ramesh-fadatare/

Connect with me on Twitter, Facebook,


LinkedIn, GitHub, andStackOverflow

Follow Me on Twitter

Follow

Facebook Likes and Shares

23K people are


Follow Share following this.

Free Courses on
YouTube
Learn Spring Boot

Learn Spring Boot 3

Learn Spring MVC

Learn Spring Data JPA

Learn Spring Boot React

Learn Java Collections

Learn Java 8 in 4 Hours

Learn 25+ Spring Boot Annotations

Spring Boot Kafka Microservices

Learn Building Spring Boot Projects

Learn Spring Boot REST API

Learn Event-Driven Microservices

Learn Spring Boot + Kafka

Learn Spring Boot + Angular

Learn Spring Boot + Thymeleaf

Top Tutorials
Learn Java

Learn Java 8

Learn Spring Boot

Learn Spring Framework

Learn Spring MVC

Learn Spring Security

Learn Spring Data JPA

Learn Spring Annotations

Learn Microservices

Learn REST API

Learn JPA

Learn Hibernate ORM

Learn JSP

Learn Servlet

Learn JUnit

Learn Thymeleaf

Top Quizzes
Java Quiz

Java 8 Quiz

Java Coding Quiz

Spring Boot Quiz

Spring Quiz

Spring MVC Quiz

Spring Data JPA Quiz

Hibernate Quiz

Miroservices Quiz

REST API Quiz

JavaScript Quiz

JavaScript Coding Quiz

React JS Quiz

Kotlin Quiz

SQL Quiz

CSS Quiz

JSON Quiz

My Bestseller
Udemy Courses
Spring 6 and Spring Boot 3 for Beginners (Includes
Projects)

Building Real-Time REST APIs with Spring Boot

Building Microservices with Spring Boot and


Spring Cloud

Full-Stack Java Development with Spring Boot 3 &


React

Testing Spring Boot Application with JUnit and


Mockito

Master Spring Data JPA with Hibernate

Spring Boot + Apache Kafka - The Quickstart


Practical Guide

Spring Boot + RabbitMQ (Includes Event-Driven


Microservices)

Spring Boot Thymeleaf Real-Time Web


Application - Blog App

Copyright © 2018 - 2025 Java Guides All rights


reversed | Privacy Policy | Contact | About Me |
YouTube | GitHub

Powered by Blogger

You might also like