Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
BlogOpen SourceTech

Create a Simple Web Application Using Servlet, JSP and JDBC

By techsupport
May 20, 2016 6 Min Read
Comments Off on Create a Simple Web Application Using Servlet, JSP and JDBC

원문보기

 

This document is based on:
  • Eclipse 4.5 MARS (ok for Eclipse 4.4 LUNA)

  • Tomcat 8.x

Document History:

  • 12-12-2014: JSP + Servlet + Filter + JSTL
  • 06-08-2015: JSP + Servlet + Filter + JSTL + JDBC
In this document, I will guide step by step how to create a simple web application with the combiantion of  Servlet + JSP +  Filter +  JSP EL + JDBC. Make sure that you’ve mastered  Servlet, JSP and  Filter and  JDBC before the start. If not, you can refer to:

Servlet:

  • http://o7planning.org/web/fe/default/en/document/12760/java-servlet-tutorial-for-beginners

Filter:

  • http://o7planning.org/web/fe/default/en/document/753859/java-servlet-filter-tutorial

JSP:

  • http://o7planning.org/web/fe/default/en/document/20397/java-jsp-tutorial-for-beginners

JDBC

  • http://o7planning.org/web/fe/default/en/document/12562/java-jdbc-tutorial

2- The principle when programming Servlet + JSP

These are the principles that you should keep in mind to be able to build a Web application using   Servlet + JSP  satisfying criteria: code is simple, easy to understand and easy to maintain.
The principles:
  1. Never allow users to directly access to your JSP page.
  2. JSP is only considered as the place to display interface.
  3. Servlet acts as the controller of the application flows and  program logical processing.
  4. Open the JDBC connection and transaction management in Filter (Optional).

According to the principle 1:

Never allow users to directly access to your JSP page, it means that all user’s requests are:

  • Another source of static data (images, css, js, …)
  • Or a servlet.

Therefore, you must hide your JSP files in a place where the user can not access. For instance, set it in the WEB-INF folder or its subdirectories. In this example, I hide the jsp files in the WEB-INF/views.

When the user  requests  to a Servlet, it will dispose user’s requirements, such insert, update and query the data, eventually forward to the JSP page to display the data. Thus,  each servlet has 0 or multiple corresponding JSP pages (Usually only need 1).

Principle 2:

JSP is only considered as the place to display data, which means that you should not handle the application logic on the JSP, such as update, insert, delete, .., and not navigate on the JSP page.

3- View Demo of Web Application will do

You can preview Demo Web application will do:

4- Prepare database

In this document, I instruct you to work with one of 3 databases: Oracle, MySQL or SQL Server. You need to run scripts to create some tables and necessary data for this example.

ORACLE:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- Create table
create table USER_ACCOUNT
(
USER_NAME VARCHAR2(30) not null,
GENDER    VARCHAR2(1) not null,
PASSWORD  VARCHAR2(30) not null,
primary key (USER_NAME)
);
-- Create table
create table PRODUCT
(
CODE  VARCHAR2(20) not null,
NAME  VARCHAR2(128) not null,
PRICE FLOAT not null,
primary key (CODE)
) ;
-- Insert data: ---------------------------------------------------------------
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('tom', 'M', 'tom001');
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('jerry', 'M', 'jerry001');
insert into product (CODE, NAME, PRICE)
values ('P001', 'Java Core', 100);
insert into product (CODE, NAME, PRICE)
values ('P002', 'C# Core', 90);
-- Commit
Commit;

MYSQL:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Create table
create table USER_ACCOUNT
(
USER_NAME VARCHAR(30) not null,
GENDER    VARCHAR(1) not null,
PASSWORD  VARCHAR(30) not null,
primary key (USER_NAME)
);
-- Create table
create table PRODUCT
(
CODE  VARCHAR(20) not null,
NAME  VARCHAR(128) not null,
PRICE FLOAT not null,
primary key (CODE)
) ;
-- Insert data: ---------------------------------------------------------------
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('tom', 'M', 'tom001');
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('jerry', 'M', 'jerry001');
insert into product (CODE, NAME, PRICE)
values ('P001', 'Java Core', 100);
insert into product (CODE, NAME, PRICE)
values ('P002', 'C# Core', 90);

SQL SERVER:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Create table
create table USER_ACCOUNT
(
USER_NAME VARCHAR(30) not null,
GENDER    VARCHAR(1) not null,
PASSWORD  VARCHAR(30) not null,
primary key (USER_NAME)
);
-- Create table
create table PRODUCT
(
CODE  VARCHAR(20) not null,
NAME  VARCHAR(128) not null,
PRICE FLOAT not null,
primary key (CODE)
) ;
-- Insert data: ---------------------------------------------------------------
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('tom', 'M', 'tom001');
insert into user_account (USER_NAME, GENDER, PASSWORD)
values ('jerry', 'M', 'jerry001');
insert into product (CODE, NAME, PRICE)
values ('P001', 'Java Core', 100);
insert into product (CODE, NAME, PRICE)
values ('P002', 'C# Core', 90);

5- Create WebApp Project

In Eclipse select:
  • File/New/Other…
Project was created.
Add index.html
index.html
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
  <head>
     <meta charset="UTF-8">
     <title>Simple Web Application</title>
  </head>
 
  <body>
 
     <h2>Simple Login Web Application using JSP/Servlet</h2>
     
     <ul>
        <li><a href="home">Home</a></li>
        <li><a href="login">Login</a></li>
        <li><a href="productList">Product  List</a>
     </ul>
     
  </body>
</html>

6- Configuring the runtime environment

The application needs to run on a WebServer,  such as  Tomcat Server, you can refer to download and declaration instructions of  Server Tomcat in  Eclipse at:
  • http://o7planning.org/web/fe/default/en/document/19569/configuring-tomcat-server-in-eclipse
Right-click the SimpleWebApp select Properties.

7- Run application for first time

Right-click on SimpleWebApp, select:
  • Run As/Run on Server
Application has been run:
OK, here everything is fine. We’ll start programming a real Web application.

8- Download and declare JDBC library

 

Author

techsupport

Follow Me
Other Articles
Previous

사라져 가는 온라인, 오프라인 결제의 경계선, o2o, VAN, PG에 대한 이야기

Next

header(php)

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme