0% found this document useful (0 votes)
21 views12 pages

Java Unit 1

The document provides an overview of Java EE (Enterprise Edition) and Servlets, detailing the architecture, technologies, and lifecycle involved in developing enterprise applications. It covers the evolution of Java EE, the role of Glassfish server, and the advantages of using Servlets for dynamic web content. Additionally, it discusses the Java Servlet API and its components, emphasizing the importance of modularity and scalability in enterprise applications.

Uploaded by

shivambind9098
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)
21 views12 pages

Java Unit 1

The document provides an overview of Java EE (Enterprise Edition) and Servlets, detailing the architecture, technologies, and lifecycle involved in developing enterprise applications. It covers the evolution of Java EE, the role of Glassfish server, and the advantages of using Servlets for dynamic web content. Additionally, it discusses the Java Servlet API and its components, emphasizing the importance of modularity and scalability in enterprise applications.

Uploaded by

shivambind9098
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/ 12

<br>

UNIT I
CHAPTER

Understanding Java 2 EE
and Servlet

Syllabus
Understanding Java EE : What is an Enterprise Application? What is java enterprise
edition? Java EE Technologies, Java EE evolution, Glass fish sever.
Java EE Architecture, Server and Containers : Types of Systerm Architecture, Java
EE Server, Java EE Containers.
Introduction to Java Servlets: The Need for Dynamic Content, Java Servlet
Technology, Why Servlets ? What can Servlets do?
Serviet API and Lifecycle : Java Servlet API, The Servlet Skeleton, The Servlet Life
Cycle, A Simple Welcome Serviet.
Working With Servlets : Getting Started, Using Annotations Instead of Deployment
Descriptor.
Working with Databases : What Is JDBC? JDBC Architecture, Accessing
Database,
The Serviet GUl and Database Example.

Sylabus Topic : Understanding Java EE and What is an Enterprise Application

1.1 Understanding Java EE


What is an Enterprise Application?

An eise aphcion is computing model in web environment. An enterprise


yii ix basically a business zpplicatiön. Enterprise Java
refers to Java applications

Scf cterprise application.


<br>

Understanding Java 2 EE E
Enterprise Java (MU B.Sc. IT- Sem-V) 1-2

Meeting non-functional system requirements


scalability.
Availability, reliability. enhance ability. performance, reusabii

interoperability.
Timely development and deployment.
Edition?
Syllabus Topic:What is Java Enterprise
i.1.2 What is Java Enterprise Edition?
J2EE defines the standard for developing multitier enterprise applications.
It stands for Java 2 Platform Enterprise Edition.
Sun/Oracle f
J2EE is a platform-independent, Java-centric environment from
developing, building and deploying Web-based enterprise applications online.
The J2EE platform consists of a set of services, APIs, and protocols that provide tt
functionality for developing multi-tiered, Web-based applications.
It simplifies enterprise applications by :
Standardized modular components.
Providing a complete set of services to those modular components.
Automatically handling many details of application behavior without compler

programming.
:
Syllabus Topic Java EE Technologies

1.1.3 Java EE Technologies


Web Applications Technologies
Java Servlets
Java Server Pages (JSP)
Enterprise JavaBeans (EJB)
Facelets
Java Server Pages Standard Tag Library[JSTL
Standard services and supporting technologies
Tava database connectivity(JDBC) data access API
Java Messaging Service (JMS)
Remote Method Invocations (RM)
<br>

Enterprise Java (MU B.Sc. IT - Sem-V) 1-3 Understanding Java 2 EE& Serviet

Extensible Markup Languages(XML)


Java IDL (Interface Description Language)
Java Mail
Java Security
CORBA technology
Web services technologies

SOAP and RESTful


Java API for XML Registries[JAXR]
Security Technologies
JACC
JASPIC

Syllabus Topic : Java EE Evolution

1.1.4 Java EE Evolution


J2EE an open standard umbrella for anything Java-related.
For designing, developing, assembling, and deploying component-based enterprise
applications.
Separation of business logic from presentation.
Reusability, enhance ability, scalability, interoperability.
Simplifies the complexity of a building n-tier application.
Standardizes an APIbetween components and application server container.
J2EE Application Server and Containers provide the framework services.

Syllabus Topic : Glassfish Server


1.1.5 Glassfish Server

Glassfish is an Application Server. It is also used as a Web Server.


Application servers enable the development of multi-tiered distributed applications.
They
are also called "middleware.

An application server acts as the interface between the databases,


the web servers and the
client browsers.
<br>

Understanding Java 2 EE& Serviet


Understanding Java 2EE& Enterprise Java (MU B.Sc. IT- Sem-V) 1-5
S
14
BSC.IT-Sem-V)
Enterprise Java (MU Contalners, Types of Systen J2EE J2EE
and Appication 1
Application 2
Architecture, Server
Syllabus Topic: Java EE Architecture Appication Dynamic Client Client
HTML Pages Machine
Containers Client Tier
Server and
1.2 Java EE Architecture,
JSP Pages Web
Architecture Tier
1.2.1 Types of System J2EE
Server
1. Single-tier Architecture Machine
Enterprise Enterprise Business
run on single computer system.
written and Beans Beans Tier
In this kind of architecture application is
Database
2.
TwO-tier Architecture EIS Server
Ipatabase Database Tier Machine
Architecture.
It is also called as Client-Server

Interaction of two-tier architecture Fig. 1.2.2: Three-tier Architecture


accesses web pages through web browser.
Client : Simply 4. Four-tler Archltecture
: Retrieves htm/JSP/servlet documents.
Server There are mainly four layers in this architecture:
Client-tier
HTME
3 page Web-tier
Object EIS-tier
Intemet!
Client TCP/IP Business-tier
HTML
Documents Client- tier Web-tier
J2EE Server

Browsers
Fig. 1.2.1: Client/Server Architecture Servlet JSP Page
EIS-tier

3. Three-tier Archltecture Web Container

There are mainly three layers in this architecture Application


Database
Cllent
Client Presentation
Application Enterprise
(Enterprisa
HTML or Java applets deployed in Browser Client Bean Bean
Container
XML documentations transmitted through HTTP Business-tier
Machine EJB Container
Java clients running in Client Java Virtual Machine (JVM)
llent
Presentation Logic
Fig. 1.2.3: Four-tier Architecture
Servlets or JavaServer Pages running
in web server
Application Logic
Enterprise JavaBeans running in Server
<br>

Understanding Java 2EE


&Sa
1-6 Enterprise Java (MU B.Sc. IT - Sem-V) Understanding Java 2 EE & Servlet
- Sem-V) 1-7
Enterprise Java (MU B.Sc. IT

Following are containers available in Java EE:


5.Multi-tier Architecture

Tiers separate functionality: Java EE contalners


Data Schema
Business Logic,
Presentation Logic, ) without
changingthe rest
Easier upgrade since one tier
can be changed 1) EJB container

cost
Lower deployment and maintenance 2) Web container

More flexible 3) Application client container

Morc extensible (can add functionality).


4) Applet container
Architecture with J2EE'
n- tier
Fig. 1.2.5: Java EE containers
EIS Tier
Appllcatlon Server
EJB container Manages the execution of enterprise beans for Java EE applications.
:
Clients 1)
server.
Container Beans Enterprise beans and their container run on the Java EE
RDBMS
pages, servlets, and some EJB
RMI/ IIOP 2) Web container : Manages the execution of web
Java Data Base run on
Clients components for Java EE applications. Web components and their container
network
the Java EE server.
VT 3) Application client container Manages the execution of application client
:

C++,VB.. PeopleSoft on the client.


components. Application clients and their container run
HTTP
Clients SAP R/3
a web browser and
4) Applet container : Manages the execution of applets. Consists of
HTML Web Server
a Java Plug-in running on the client together.
Legacy :

Browser Systems Containers provide services for


Clients
Security

Fig. 1.2.4 :n-tier Architecture Transaction


Persistence
Syllabus Toplc Java EE Server
:

Concurrency
1.2.2 Java EE Server Availability

The runtime portion of aJava EE product. A Java EE server provides EJB and wel Lifecycle Management
as
Contaipners. The container also manages non-configurable services such

Syllabus Toplc: Java EE Contalners enterprise bean and servlet lifecycles


database connection resource pooling
1.2.3 Java EE Containers
data persistence
Containers are the interface between a component access to the Java EE platform APIs
and the low-level, platform-speci
functionality that supports the component.
<br>

Underetanding Java
1-8 28E&B
Enterprise Java (MU B.SC. IT- Som-V).
E Need for Dynamlc Dordot
Servlots and The
B

Cont Entorprlso Java (MU ES,Sc. IT Som-V) Undsrntarnding Java 2 EE


Java
Syllabus Toplc: Introductlon to
tttrek

Syllabus Toplc :Why Sorvlots ?


1.3 Introduction to Java Servlets orot

The Need for Dynamic Content I.3.3 Why Sorvlots ?


1.3.1

Ihere are many advantages of Servlct


to
over CGI, The web containcr

Servlct.
crentes
threadh
, Servlet technolopy is uned to create a wch application (ressdes at server side and

handling he multiple requests the penerates a dynamic wcb pae).


over the Proccsses such
as they sharc a COnmon memo,
Thrends have many bencfits anguage, Belore Servlet, CGI
arc low. Servlet tcchnology is robust and scalable hecause of java
communication betwcen thc threads
arca, lightwcight, cost of (Cominon Gateway Interfacc) scripting languagc
was coinon % 4 terver-side
Thc advantagcs of Scrvlct
are as follows : programmning language. Howevcr, therc werc any disadvantages to this technology,J

Advantagos of Servlot
Syllabuo Toplc What can Serviste do?
:

Bettor performanc0
What can Sorviets do?
1.
1.3.4
to cxtcnd the capabilitics of
servlet is a Java programming languaye class that uscd
2. Portability is
A

servers that host applicatiops accesscl by mcans of a rcquest-Csponse programming


3. Robust
Inodel.
4. Socuro requcst, they are commonly used to cxtend
Although scrvlets can respond to any type of
servers. For such applications, Java Scrvlet techuology
Fig. 1.3.1:Advantages of Servlet the applications hosted by weh
lefines IHTTP-specific servlct classes.
Better performance : because it creatcs a thread for each request, not process,

$yllabus Toplc Java Sorvlot APl and Llfocycle


:
2. Portability : becauscit uses Java language.
3. Robust : JVM manages Servlets, so we don't nccd to worry about the memon
leak, garbage collection, ctc. 1.4 Servlet APl and Lifecycle
Secure : because it uses java language.
1.4.1 Java Servlet API
Syllabus Toplc : Java Servlet Technology packagcs reprcsent interfaces and classcs for
The javaX.scrvlct and javax.servlet.htp
1.3.2 Java Servlet Technology servlct api.
Servlet can be described in many ways, depending on the context. many interfaces ad classcs that are uscd by the
Thc javaxservlet packagc contains
Servlet is a technology which is used to crcate a web application. are not specific to any protocol.
servlet or web container, These
arc responziblc for
Servlet is an API that provides many interfaces and classes including documentation. contiains interfaces and classes that
The javax,servlet.http package
Servlet is an interface that must be implemented for creating any
Servlet. htp re<questsonly.
Servlet is a class that extends the capabilities of the ervers package.
and responds to the incoming - Let's scc what arc the interfaces of javax .servlet
requests. It can respond to any requests.
Servlet is a web component that is deployed on
the server to create a dynamic
wcb page.
<br>

thterataNting Java ke

Uteattng tavaPEN Dovlet


Intertavs n javas,serv pekage
Sylabua Tople : The Borvlet keleton
ServletRqest
Revquestisitwher 1,4.2 The Servlet Skeleton
scover voudors.
Tle Sun Micosvxlem doinN miqe standand to be followel by all tho
a

Sorvehatext
Le's seo tho sheleton stuci hatmt bo ollowd towte ihe servlet.
Web ap)
(Context-Roo)
13, ServletRequesaAltributelistener

WelrINF
13, Servleontexthistenr
(lases in javax,servlet package : Clasaos
They are as follows
ane mny clasNCS in javav.servlet pakage,
her ServdetluputStream cluan titaa
1. GenerieServlet
ServletRequestWraper Wb xnl
3. ServleOulutSteam HTML
ServletRequestivent
$. ServleResponse\Wraprr
ote.)
ServletRequestAributelivent Static Resourcos (og. lmajos,C99
7. SenvletContextvent
Sorvletixception Fig. 14.1:Skeleton Structure
9, ServlerContextAttributeEvent 0.
Llto Cycle
Syllabus Toplc Tho Sorvlet
:
II. UnavailableExeepion
Interfaces in javas.servlet.http package Tho Sorvlot Lito Cycle
are as follows
:
1.4.3
javax,servlet.httn package, Thèy
There are many interfaces in
HupServletResponse The Init() Method invoked by the weh
2. nnethod of servlet und
I. HupServletRequcst Initializes the servlet.
It is the life cycle
HupSessionlistener once.
HtpSession containcr only
6. HupSessionBindingListener
HtpSessionAttributeListener Syntax
5.
8. HtpSessionContext (deprecated now) ServletException
7. HtpSessionActivationListener publie vokl iit() throNs

Classes in javaxservlet.http package


package. They are as follows
:
nitialization code.
There are many classes in javax.servlet.bttp
1. HtpServlet
2 Cookie
Method requesf by the web
HupServletResponseWrapper Tho service() invoked at each
incoming rcquest. It is
HtpServletRequestWrapper 4.
3.
Provides
response for he
5. HtpSessionEvent 6. HtpSessionBindingEvent
containcr.
7. HttpUtils (deprecated now)
<br>

Understanding Java
1-14 2EE
-
B.Sc. IT Sem-V)
Enterprise Java (MU
IT - Sem-V) Understanding Java 2 EE & Servlet
Getting Started Enterprise Java (MU B.Sc. 1-15
Syllabus Topic:
response.setContentlype('text/html");

1.5.1 Geting Started servlet in Netbcans. PrintWriter out =response.getWriter();


create web application using
are the steps to out.print("<html> <body>");
Following
menu. out.print("<h3>Hello Servlet </h3>"):
Go to the tile
new project.
out.print('</body> </huml>");
Select
Select categories as java web.
name and click on next button.
Provide project
on finish button.
work and click What is JDBC
Select framework which vou want to Syllabus Topic :Working With Databases and
Instead of Deployment
Descrint
Annotations
Syllabus Topic: Using Working with Databases
Descriptor .6
Using Annotations
Instead of Deployment
1.5.2 deployment 1.6,1 What Is JDBC?
you use annotation, descig interact with the
Annotation represents the
metadata. If
run in component that enables java application to
tomcat7 as it will
not
theprevia JDBC Driver is a software
you should have interfaces in the JDBC API, for interacting with
(web.xml file) is not required. But database. They implement the defined
annotation is used to map the servlt with the specte
versions of tomcat. @WebServlet
database server.
and to interact
name.
For example, using JDBC drivers
enables to open database connections
results with Java.
A simple example of annotation: or database commands then recciving
with it by sending SQL
va io.10Exception; with their behaviors
ümport j with JDK contains various classes
The Java.sql package that ships drivers. A third party
are done in third-party
import javaio.PrintWriter; defined and their actual implementations
driver.
interface in their database
vendor implements the java.sql.Driver
import javak.servlet.ServletExceplion; 1.6.2 JDBC Drivers Types
import javax.servlet.anhotation.WebServlet; vary because of the wide varicty of operating
systems and
JDBC driver implementations types into fur
implemnentation
impon javax.servlet.http.HtpServlet; hardware platforms in which Java
operates. Sun has divided the
:

import javax.servlet.http.HttpServletRequesl; is explained below


categories, Types 1, 2, 3, and 4, which
import javax.servlet,http.HtpServletResponse;
Types of JDBC Drivers

@VebServlet(/Simple") 1. JDBO-ODBC Bridge Driver


public class Sinple extends HupServlet {
2. JDBC-Native API
private static final long serialVersionUID = lL;
3. JDBC-Net pure Java

protected void doGet(HtpServletRequest recjiest, HipServletlesponse response) 4. 100% Pure Java (Thin
Driver)

throws ServletException, 1OException {


Fig. 1.6.1:Types
of JDBCDrivers
<br>

Tye 1:D9C-0DSC Bridge Driver

Aoirzicrn

Ciet Waire

Fie 63:Ktre
M
árive

Disatertage
KEtages

Type 3:JDBCNet purs lavs

Catatas

Foicatr
<br>

2
EE & S
Understanding Java
1-18 Understanding Java 2 EE & Servlet
B.SC. IT- Sem-V) Enterprise Java (MU B.Sc.IT- Sem-V) 1-19
Enterprise Java (MU
a
to the data source. This requires
the two-tier model, Java application talks directly
a
In
Advantage can perform man source being accessed.
application server that JDBC driver that can communicate with the particular data
library is required because of
No client side database or other data sourcc, and the results of
A user's commands arc delivered
to the
balancing, logging ctc.
tasks like auditing, load user.
those statements are sent back to the
a
may on another machine to which the user is connected via
Disadvantages The data source be located
as the
a configuration, with the user's machine
on client machine. network. This is referrcd to as clienUserver
Network support is required source as the server.
middle tier. client, and the machine housing the data
coding to be done in the
Requires database-spccific Java Application Client Machine
requires dataha
becomes costly because it The network can be an intranet, which, for
Maintenance of Network Protocol driver a JDBC
in the middle tier. example, connects employees within
specific coding to be done DBMS-proprietary protocol
Driver) corporation, or it can be the Internet.
Type 4: 100% Pure Java (Thin are sent
driver communicates directly with
the vendo. In the three-tier model, commands Database server
In a Type 4 driver,
a pure Java-based DBMS
performance driver available for th. to a "middle tier" of services, which then
is the highest
database through socket connection. This source. Fig. 1.6.6: Two-tier Architecture for
vendor itself. sends the commands to the data
database and is usually provided by the Data Access
tier.
and sends the results back to the middle
The data source processes the commands very attractive
Jdbc API user. directors find the three-tier model
which then sends them to the MIS
to maintain control over access and the kinds of
because the middle tier makes it possible
updates that can be made to corporate data.
Database many
of applications. Finally, in
Java Thin driver Another advantage is that it simplifies the deployment
Application
cases, the three-tier architecture can provide performance advantages.

Client Machine
Java applet or Client Machine (GUI)
HTML browser

HTTP, RMI, CORBA, or other calls


Fig. 1.6.5: Thin Driver
Application Server
Server machine
Advantage (java)
(business logic)
Better perförmance than all other drivers. JDBC

No software is required at client side or server side. DBMS-proprietary protocol

Dlsadvantáge:
Database server
Drivers depend on the Database DBMS

Fig. 1.6.7: Three-tier Architecture for


Data Access
Syllabus Topic :JDBC Archítecture
:

1.6.3 JDBC Architecture The JDBC API is comprised of two Java packages OR (NIC
IGA

The JDBC API supports both two-tier and threc-tier processing java.sq!
models for database
access. javax.sql.
<br>

Understanding Java 2 EE&s.


1-20 2 EE & Servlet
Enteprise Java (MU B.SC. IT- Sem-V) B Enterprise Java (MU B.Sc. IT- Sem-V) 1-21 Understanding Java
packa
interfaces, and Cxceptions in the java.sql
The following are core JDBC classes, 4. Process the results.

g 5. Close the database connection.


Driver
respondin.
point for database connectivity by
Driver interface gives JDBCa launching Syllabus Toplc :The Servlet GUl and Database Example
information about the implemecntatio
DriverManager connection requests and providing
1.6.5 The Servlet GUI and Database Example
question.
Create a registration servlet in Java using JDBC. Accept the details such as Username,
r DrlverManager
Password, Email, and Country from the user using HTML Form and store the registration
user and drivers.
The DriverManager class acts as an interface between details in the databasc.
Tt
keens track of the drivers that are available and handles establishing a connectio ereate databasce LoginDB:
between a database and the appropriate driver. use LoginDB;
The DriverManager class maintains a list of Driver classes that have registere
themselves by calling the method DriverManager.registerDriver). create table user(username varchar(20) PRIMARY KEY, password varehar(20), email
varchar(20), country varchar(20);
T Connection
A
Connectionis the session between java application and database.
insert into user values ('adnin','admin',;[email protected], India');
The Connection interface is a factory of Statement, Prepared Statement, an
DatabaseMetaData i.e. object of Connection can be uscd to get the object of Statemen
select * from user;
and DatabascMetaData.
The Connection interface provide many methods for transaction management like

commit 0, rollback ) ctc. O


Practical 1C - NetBens 1DE 82

Statement Interface File Edt View Navigate Source Refactor Run D

The Statement interface provides methods to execute queries with the database.
The statement interface is a factory of ResultSet i.e. it provides factory method to get the
object of ResultSet. Projects n Fles Services
Practca_1C
Web Pages
Syllabus Toplc: Accessing Database
E- Sorce Padages
1.6.4 Accessing Database <defait padage>

The process of retrieving information from a database Confgi Add Project...


via JDBC involves thesc five basic
steps: Add Library..
1. Register the JDBC driver with the driver manager. Add JARJfolder..
2. Establish a databasc connection.
Proçerties
3. Execute an SQL statement.
<br>

Understanding Javn 2 EE &

Enterprise Java (MU B.Sc. IT- SomV) 1-23 Undorstanding Java 2 EE & Servlot
Rkrsadelmxulesketumysql-conncctor-java-S. I.23.:
Adi jar tilk ehan PreparedStatenment pst conpreparmstatenent("ineert into user values(?.?2,2)"):
index.htm! pat.aetString(l.id):
<htm> pst.setString(2,p*):
<head> pat.setString(3,em):
Sizle>Registrstiet Pute<ite> pst.setString(4.co):
<hesd> int row pst.executelUplate(0:
<hdy>
Khrm ation RegisterSevlet
= > oul.println("<hl>"+ow+" Iuserted Suecesfullyyyyy"):
<HISWleene to Registration pg<Hl>
Eter t'set Name <input type='text" name="txtUid"> <br> catch(Exception e)

Enter Pessward Cinput type=password" nane "txtPass"><br {out.println(e):}


Eater Enil <input type=text" name "xtEmail" ><hr>
Enter Country <input type="text" name="tstCon" ><br>
Sinput type='rset" >
sinput type="uhmit' value="REGISTER" > Review Questions
Klorm> Q. 1 Explain 3-tier architecturo of J2EE in dotail.
Kbody Writo a program to show the use of calculator using servlet.
Q.2
Khtmi
Q.3 What are the types of driver in JDBC? Explain.
RegisterSerlet.java
Q. 4 Explain use of following in JDB0.
niport java.ia*
1. Driver 2. DriverManager 3. Connection
import java.sql.;
impot jayaxservlet.; 4. Resultset 5. Prepared Statement

import jaar.servletihtp,"; Q.5 What is J2EE? Explain in brief.


Q. 6 Write a short note on J2EE architecture.
publie rlass RegisterServlet extends HtpServlet { O.7 Explain servlet life cycle and servlet API.
public void doGet(HtpServletRequest request, HtpServletResponse response) Q.8 Write a program to use of login form using servlet.
throws ServletExcepuon, IOExceptivn { response.set
ContentType('texthtml;charset =UTF-8"); Q.9 Explain architecture of JDBC in detail.
PrintWriter out = response.get Writer): are servlets? What are the advantages
Q. 10 WNhat of servlets over CGi?
String id requesi.getParanieter('xtUid"):
O. 11 Write a servlet application to find the sum of digits of the number entored by the user
String ps requést.getl'araneter("xtPass"): through the HTML form.
em = request.getParameter('IxtEanail"): Q. 12 Write a JDBC program that inserts values in database.
Sting co = request.etParameteruCon"); [TABLE NAME:Employee, FIELDS: Empid, Name, Dept, Designation]
try Class.forNamet'eom.my sqljdbe.Driver"root","root"); Chapter Ends...
Connection con =DriverManager
getConnection( jdbe:nysql://localhost:3306/logindb");

You might also like