57 - Aritra Dutta

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Industrial Training

INDUSTRIAL TRAINING ON
JSP, SERVLET AND JDBC FOR BEGINNERS: BUILD A
DATABASE APP

➢ NAME:- ARITRA DUTTA


➢ ROLL NO:- 16900320057
➢ DEPT:- ELECTRONICS AND COMMUNICATION
➢ SEMESTER:- 7TH
➢ COLLEGE NAME:- ACADEMY OF TECHNOLOGY
➢ SUBJECT CODE:- EC781

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

3. CHAPTER-3:INDUSTRIAL TRAINING DETAILS

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.

Properties of Servlets are as follows:


Servlets work on the server-side.
Servlets are capable of handling complex requests obtained from the webserver.
The server-side extensions are nothing but the technologies that are used to create dynamic Web
pages. Actually, to provide the facility of dynamic Web pages, Web pages need a container or Web
server. To meet this requirement, independent Web server providers offer some proprietary
solutions in the form of APIs(Application Programming Interface). These APIs allow us to build
programs that can run with a Web server. In this case, Java Servlet is also one of the component
APIs of Java Platform Enterprise Edition which sets standards for creating dynamic Web
applications in Java. Before learning about something, it’s important to know the need for that
something, it’s not like that this is the only technology available for creating dynamic Web pages.
The Servlet technology is similar to other Web server extensions such as Common Gateway
Interface(CGI) scripts and Hypertext Preprocessor (PHP). However, Java Servlets are more
acceptable since they solve the limitations of CGI such as low performance and low degree
scalability.CGI is actually an external application that is written by using any of the programming
languages like C or C++ and this is responsible for processing client requests and generating
dynamic content.
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the
database. It is a specification from Sun microsystems that provides a standard abstraction(API or
Protocol) for java applications to communicate with various databases. It provides the language
with java database connectivity standards. It is used to write programs required to access databases.
JDBC, along with the database driver, can access databases and spreadsheets. The enterprise data
stored in a relational database(RDB) can be accessed with the help of JDBC APIs.JDBC is an
API(Application programming interface) used in java programming to interact with databases. The
classes and interfaces of JDBC allow the application to send requests made by users to the specified
database.Enterprise applications created using the JAVA EE technology need to interact with
databases to store application-specific information. So, interacting with a database requires efficient
database connectivity, which can be achieved by using the ODBC(Open database connectivity)
driver. This driver is used with JDBC to interact or communicate with various kinds of databases
such as Oracle, MS Access, Mysql, and SQL server database. There are generally four main
components of JDBC through which it can interact with a database. They are as mentioned below:

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.

3. JDBC Driver manager: It loads a database-specific driver in an application to establish a


connection with a database. It is used to make a database-specific call to the database to process
the user request.
4. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being
performed by JDBC Drivers.

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.

CHAPTER-2: THEORY/THEORETICAL BACKGROUND


6| Page
Industrial Training

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.

● Creating SQL or MySQL statements.

● Executing SQL or MySQL queries in the database.

● Viewing & Modifying the resulting records.


Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for
portable access to an underlying database. Java can be used to write different types of executables,
such as −
● Java Applications

● Java Applets

● Java Servlets

● Java ServerPages (JSPs)

● Enterprise JavaBeans (EJBs).


All of these different executables are able to use a JDBC driver to access a database, and take
advantage of the stored data.
JDBC provides the same capabilities as ODBC, allowing Java programs to contain
databaseindependent code.
Before moving further, you need to have a good understanding of the following two subjects −
● Core JAVA Programming ● SQL or MySQL Database
The JDBC API supports both two-tier and three-tier processing models for database access but in
general, JDBC Architecture consists of two layers −

● JDBC API − This provides the application-to-JDBC Manager connection.

● JDBC Driver API − This supports the JDBC Manager-to-Driver Connection.


The JDBC API uses a driver manager and database-specific drivers to provide transparent
connectivity to heterogeneous databases.The JDBC driver manager ensures that the correct driver is
used to access each data source. The driver manager is capable of supporting multiple concurrent
drivers connected to multiple heterogeneous databases.Following is the architectural diagram,
which shows the location of the driver manager with respect to the JDBC drivers and the Java
application −

7| Page
Industrial Training

The JDBC API provides the following interfaces and classes −

● DriverManager − This class manages a list of database drivers. Matches connection


requests from the java application with the proper database driver using
communication sub protocol. The first driver that recognizes a certain subprotocol
under JDBC will be used to establish a database Connection. ● Driver − This interface
handles the communications with the database server. You will interact directly with
Driver objects very rarely. Instead, you use DriverManager objects, which manages
objects of this type. It also abstracts the details associated with working with Driver
objects.

● 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>

It will print 10 on the browser.

10 | Page
Industrial Training

Follow the following steps to execute this JSP page: o Start the
server

o Put the JSP file in a folder and deploy on the server


o Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example,
http://localhost:8888/myapplication/index.jsp

11 | Page
Industrial Training

SERVLETS:-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.

Servlet can be described in many ways, depending on the context.

o Servlet is a technology which is used to create a web application. o Servlet is an


API that provides many interfaces and classes including documentation. o
Servlet is an interface that must be implemented for creating any Servlet. o
Servlet is a class that extends the capabilities of the servers and responds to the
incoming requests. It can respond to any requests.

o Servlet is a web component that is deployed on the server to create a dynamic


web page.

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.

Dynamic website:Dynamic website is a collection of dynamic web pages whose


content changes dynamically. It accesses content from a database or Content
Management System (CMS). Therefore, when you alter or update the content of the
database, the content of the website is also altered or updated.

Dynamic website uses client-side scripting or server-side scripting, or both to generate


dynamic content.Client side scripting generates content at the client computer on the
basis of user input. The web browser downloads the web page from the server and
processes the code within the page to render information to the user.In server side
scripting, the software runs on the server and processing is completed in the server
then plain pages are sent to the user.

13 | Page
Industrial Training

CHAPTER-3:INDUSTRIAL TRAINING DETAILS


From this UDEMY course I learned a lot of about JSP, servlet and JDBC. Previously I
have knowledge on java and these are the advance field of java and these are java
enterprise edition. In JDBC section I learn how we make connection between java and
SQL. In JDBC there are 4 types of drivers. 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.

● Creating SQL or MySQL statements.

● Executing SQL or MySQL queries in the database.

● Viewing & Modifying the resulting records.

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:

1. the output of a CGI program that ran on the Web server

2. it could be a file, with some other information included by the server, using
server side includes or ASP.

3. it could be the output of a servlet.

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

You might also like