Professional Documents
Culture Documents
57 - Aritra Dutta
57 - Aritra Dutta
57 - Aritra Dutta
INDUSTRIAL TRAINING ON
JSP, SERVLET AND JDBC FOR BEGINNERS: BUILD A
DATABASE APP
1| Page
Industrial Training
ABSTRACT
The JDBC (Java Database Connectivity) API is the industry standard for database- independent connectivity
between the Java programming language and a wide range of databases. The JDBC API provides a call-level
API for SQL-based database access. JDBC technology allows you to use the Java programming language to
exploit "Write Once, Run Anywhere" capabilities for applications that require access to enterprise data. The
project uses JDBC technology in order to provide the end user with a uniform interface that enables them to
query multiple databases located in different servers. Database servers used in the project are PostgreSQL
and MySQL. Using the project, the user is able to access and retrieve information from databases within
short span of time while maintaining the security of the databases. This project has a wide variety of
applications because this would help to implement distributed computing in the future.
PROJECT OBJECTIVE: to provide the user with an efficient and interactive interface that would query
multiple databases ← a more comprehensive approach to access the database and to reduce response time
and cost. ← To save users time by providing user with uniform GUI to query multiple database.
PROJECT DESIGN: Birla Institute of Technology and Science, Pilani – Dubai Campus
PROJECT IMPLEMENTATION WORKING The user writes the SQL query in a GUI
(Graphical User Interface) The SQL query is validated i.e. is checked for syntax and valid fields & table
name, otherwise the GUI will prompt user to check the syntax. The interface will first extract the table name
from the query Open the file “getdbname.txt” that contains database name of that particular table Then, it
opens another file “dbserver.txt” to find the database server to which the database belongs to. Then it is
connected to the particular database server using jdbc connection. It will determine whether the SQL is a
simple query or an update. The interface will execute the query and store the result in the text file named
output.txt in a tabular form. Today we all are aware of the need of creating dynamic web pages i.e the ones
which have the capability to change the site contents according to the time or are able to generate the
contents according to the request received by the client. If you like coding in Java, then you will be happy to
know that using Java there also exists a way to generate dynamic web pages and that way is Java Servlet.
But before we move forward with our topic let’s first understand the need for server-side extensions.
Servlets are the Java programs that run on the Java-enabled web server or application server. They are used
to handle the request obtained from the webserver, process the request, produce the response, then send a
response back to the webserver. Java Server Pages (JSP) is a server-side programming technology that
enables the creation of dynamic, platform-independent method for building Web-based applications. JSP
have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.
2| Page
Industrial Training
CONTENTS
1. CHAPTER-1:INTRODUCTION
2. CHAPTER-2:THEORY/THEORETICAL BACKGROUND
4. CHAPTER-4:CONCLUSION
3| Page
Industrial Training
CHAPTER-1: INTRODUCTION
JSP technology is used to create web application just like Servlet technology. It can be thought of as
an extension to Servlet because it provides more functionality than servlet such as expression
language, JSTL, etc.A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to
maintain than Servlet because we can separate designing and development. It provides some
additional features such as Expression Language, Custom Tags, etc. JavaServer Pages (JSP) is a
technology for developing Webpages that supports dynamic content. This helps developers insert
java code in HTML pages by making use of special JSP tags, most of which start with <% and end
with %>.A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role
of a user interface for a Java web application. Web developers write JSPs as text files that combine
HTML or XHTML code, XML elements, and embedded JSP actions and commands.Using JSP, you
can collect input from users through Webpage forms, present records from a database or another
source, and create Webpages dynamically.JSP tags can be used for a variety of purposes, such as
retrieving information from a database or registering user preferences, accessing JavaBeans
components, passing control between pages, and sharing information between requests, pages
etc.JavaServer Pages often serve the same purpose as programs implemented using the Common
Gateway Interface (CGI). But JSP offers several advantages in comparison with the
CGI.Performance is significantly better because JSP allows embedding Dynamic Elements in
HTML Pages itself instead of having separate CGI files.JSP are always compiled before they are
processed by the server unlike CGI/Perl which requires the server to load an interpreter and the
target script each time the page is requested.JavaServer Pages are built on top of the Java Servlets
API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC,
JNDI, EJB, JAXP, etc.JSP pages can be used in combination with servlets that handle the business
logic, the model supported by Java servlet template engines.Finally, JSP is an integral part of Java
EE, a complete platform for enterprise class applications. This means that JSP can play a part in the
simplest applications to the most complex and demanding.
Today we all are aware of the need of creating dynamic web pages i.e the ones which have the
capability to change the site contents according to the time or are able to generate the contents
4| Page
Industrial Training
according to the request received by the client. If you like coding in Java, then you will be happy to
know that using Java there also exists a way to generate dynamic web pages and that way is Java
Servlet. But before we move forward with our topic let’s first understand the need for server-side
extensions. Servlets are the Java programs that run on the Java-enabled web server or application
server. They are used to handle the request obtained from the webserver, process the request,
produce the response, then send a response back to the webserver.
5| Page
Industrial Training
1. JDBC API: It provides various methods and interfaces for easy communication with the database.
It provides two packages as follows, which contain the java SE and Java EE platforms to exhibit
WORA(write once run anywhere) capabilities.
2. It also provides a standard to connect a database to a client application.
5. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates
the JDBC method call to the ODBC function call. It makes use of the sun.jdbc.odbc package
which includes a native library to access ODBC characteristics.
JDBC:-JDBC stands for Java Database Connectivity, which is a standard Java API for database-
independent connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that are commonly
associated with database usage.
● Making a connection to a database.
● Java Applets
● Java Servlets
7| Page
Industrial Training
● Connection − This interface with all methods for contacting a database. The
connection object represents communication context, i.e., all communication with
database is through connection object only.
● Statement − You use objects created from this interface to submit the SQL statements
to the database. Some derived interfaces accept parameters in addition to executing
stored procedures.
● ResultSet − These objects hold data retrieved from a database after you execute an
SQL query using Statement objects. It acts as an iterator to allow you to move through
its data.
● SQLException − This class handles any errors that occur in a database application.
The java.sql and javax.sql are the primary packages for JDBC 4.0. This is the latest JDBC version at
the time of writing this tutorial. It offers the main classes for interacting with your data sources.
8| Page
Industrial Training
JSP:-JSP technology is used to create web application just like Servlet technology. It can be
thought of as an extension to Servlet because it provides more functionality than servlet such as
expression language, JSTL, etc.
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet
because we can separate designing and development. It provides some additional features such as
Expression Language, Custom Tags, etc.
There are many advantages of JSP over the Servlet. They are as follows:
1)Extension to Servlet: JSP technology is the extension to Servlet technology. We can use all the
features of the Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression
language and Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain: JSP can be easily managed because we can easily separate our business
logic with presentation logic. In Servlet technology, we mix our business logic with the presentation
logic.
3) Fast Development: No need to recompile and redeploy: If JSP page is modified, we don't
need to recompile and redeploy the project. The Servlet code needs to be updated and recompiled if
we have to change the look and feel of the application.
4) Less code than Servlet: In JSP, we can use many tags such as action tags, JSTL, custom tags,
etc. that reduces the code. Moreover, we can use EL, implicit objects, etc.
The JSP pages follow these phases:
o Translation of JSP Page o Compilation of JSP Page o Classloading (the classloader loads
class file) o Instantiation (Object of the Generated Servlet is created). o Initialization (
the container invokes jspInit() method).
o Request processing ( the container invokes _jspService() method). o Destroy ( the container
invokes jspDestroy() method). o jspInit(), _jspService() and jspDestroy() are the life
cycle methods of JSP.
9| Page
Industrial Training
As depicted in the above diagram, JSP page is translated into Servlet by the help of JSP translator.
The JSP translator is a part of the web server which is responsible for translating the JSP page into
Servlet. After that, Servlet page is compiled by the compiler and gets converted into the class file.
Moreover, all the processes that happen in Servlet are performed on JSP later like initialization,
committing response to the browser and destroy.
Creating a simple JSP Page: To create the first JSP page, write some HTML code as given below,
and save it by .jsp extension. We have saved this file as index.jsp. Put it in a folder and paste the
folder in the web-apps directory in apache tomcat to run the JSP page. index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP
page. We will learn scriptlet tag later.
1. <html>
2. <body>
3. <% out.print(2*5); %>
4. </body>
5. </html>
10 | Page
Industrial Training
Follow the following steps to execute this JSP page: o Start the
server
11 | Page
Industrial Training
Servlet technology is robust and scalable because of java language. Before Servlet,
CGI (Common Gateway Interface) scripting language was common as a server-side
programming language. However, there were many disadvantages to this technology.
We have discussed these disadvantages below.
There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc.
Advantages of Servlet:
12 | Page
Industrial Training
There are many advantages of Servlet over CGI. The web container creates threads for
handling the multiple requests to the Servlet. Threads have many benefits over the
Processes such as they share a common memory area, lightweight, cost of
communication between the threads are low. The advantages of Servlet are as follows:
1. Better performance: because it creates a thread for each request, not process.
2. Portability: because it uses Java language.
3. Robust: JVM manages Servlets, so we don't need to worry about the memory
leak, garbage collection, etc.
4. Secure: because it uses java language.
Static website is the basic type of website that is easy to create. You don't need the
knowledge of web programming and database design to create a static website. Its
web pages are coded in HTML.The codes are fixed for each page so the information
contained in the page does not change and it looks like a printed page.
13 | Page
Industrial Training
JSP technology is used to create web application just like Servlet technology.
It can be thought of as an extension to Servlet because it provides more
functionality than servlet such as expression language, JSTL, etc.
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to
maintain than Servlet because we can separate designing and development. It
provides some additional features such as Expression Language, Custom Tags,
etc.
Servlet technology is used to create a web application (resides at server side and
generates a dynamic web page).
Servlet technology is robust and scalable because of java language. Before Servlet,
CGI (Common Gateway Interface) scripting language was common as a server-side
programming language. However, there were many disadvantages to this technology.
We have discussed these disadvantages below.
There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc.
Static website is the basic type of website that is easy to create. You don't need the
knowledge of web programming and database design to create a static website. Its
web pages are coded in HTML.The codes are fixed for each page so the information
contained in the page does not change and it looks like a printed page.
14 | Page
Industrial Training
CHAPTER-4: CONCLUSION
This module explained what servlets are and placed them in context. You know how
the Web browser and Web server work together to get information to the user. The
browser sends a GET request and some request headers. The server sends some
response headers and then a stream of HTML.
Many times the HTML is from a file, however, it can also be one of the following:
2. it could be a file, with some other information included by the server, using
server side includes or ASP.
We know that HTTP is stateless, so that each request is independent of the requests
that came before. When your browser sends a GET to the server, it doesn’t tell the
15 | Page
Industrial Training
server that you recently requested a related page, it only sends a GET. However, the
GET request can include parameters, and smart programming on the server can use
those parameters to maintain state. You have observed what CGI, SSI, and ASP are,
and how they compare to servlets. A servlet is a Java class that inherits from
javax.servlet.http.HttpServlet. In this course we learn the fundamental aspects of
JDBC and get a clear understanding of how it operates. You learn how to apply the
numerous features JDBC provides to large-scale projects and systems, and see how it
works with other Java-based technologies such as the Java Naming and Directory
Interface (JNDI), Java Server Pages (JSP), and Enterprise Java Beans (EJB).
16 | Page