Download Java Code Snippets: User Account, Product, and Database Connection and more Essays (high school) Computer science in PDF only on Docsity!
Create a Simple Java Web Application Using Servlet, JSP and JDBC
View more Tutorials:
Java Servlet/Jsp Tutorials
1- Introduction
2- The principle when programming Servlet + JSP
3- View Demo of Web Application will do
4- Prepare database
5- Create WebApp Project
6- Configuring the runtime environment
7- Run application for first time
8- Download and declare JDBC library
9- Download and declare JSTL library
10- Javabean classes simulated tables in the database
11- Database Connection Utility classes
12- The utility class & manipulate data
13- Create Servlet Filter connect to Database
14- Servlet Filter reads Cookies to automatically login
15- EncodingFilter Servlet
16- Pages reuse
17- Home Page
18- Login Page - LoginServlet
19- Product List Page
20- Add Product Page
MENU
21- Edit Product Page
22- Delete Product
1.6k
Shares
1- Introduction
This document is based on:
Eclipse 4.5 MARS
Tomcat 8.x
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:
Java Servlet Tutorial for Beginners
Servlet Filter:
Java Servlet Filter
JSP:
Java JSP Tutorial for Beginners
JSP Standard Tag Libs (JSTL)
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:
ORACLE:
MYSQL:
-- 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;
-- 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)
In Eclipse select:
File/New/Other...
Add index.html
index.html
Simple Web Application
Simple Login Web Application using JSP/Servlet
Home Login Product List
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:
Installing and 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
You have to download JDBC library to driving the connection with the Database. In this document, I
download both of 3 JDBC libraries for Oracle, MySQL, SQL Server, in practice, you only need JDBC
library corresponding to the type of database you are using.
You can see download instruction of JDBC driver at:
JDBC Driver Libraries for different types of database in Java
‘Or download here:
some-jdbc-drivers.zip (MySQL + SQL Server + Oracle) o7planning link.
Results downloaded:
Copy these libraries into the WEB-INF/lib:
http://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api
Copy 2 jar files that you just downloaded into the / WEB-INF/lib:
10- Javabean classes simulated tables in the database
Create 2 JavaBean classes, wherein each class simulated a table in the database:
UserAccount.java
package org.o7planning.simplewebapp.beans;