Overview of JEE Technology

49
© People Strategists www.peoplestrategists.com Slide 1 of 48 Overview of JEE Technologies

Transcript of Overview of JEE Technology

© People Strategists www.peoplestrategists.com Slide 1 of 48

Overview of JEE Technologies

© People Strategists www.peoplestrategists.com Slide 2 of 48

Objectives

In this session, you will learn to:

Describe website and Web application

Identify Enterprise Application

Explore Servlet API

© People Strategists www.peoplestrategists.com Slide 3 of 48

A Web page is an electronic document, which is created using HTML and is translated by the Web browser.

A Web pages can be :

Static

Dynamic

A website is a collection of one or more Web pages, which are hosted on a Web server and are accessed by using client applications, such as a Web browser.

A Website usually refers to the front-end interface through which the users interacts with a business online.

A website consists of:

HTML and other graphic files

Audio and Video files

Other static files

Describing a Website and Web Application

© People Strategists www.peoplestrategists.com Slide 4 of 48

A Web application:

Is a program that is stored on a remote server and delivered over the Internet through a Web browser using various protocols, such as HTTP.

Is a Website with dynamic functionality on the server and can be controlled by the users.

To access a Web application:

A client sends a request to the Web server.

A Web server processes the request, generates the response, and sends the response to the client.

Describing a Website and Web Application (Contd.)

© People Strategists www.peoplestrategists.com Slide 5 of 48

What is an enterprise application?

Identifying an Enterprise Application

© People Strategists www.peoplestrategists.com Slide 6 of 48

An enterprise application is the term used to describe applications or software that a business would use to assist the organization in solving enterprise problems.

An enterprise application is an application that is:

Large in size

Distributed

Secure and scalable

Highly available

Accessible through multiple types of clients

Important aspects of an enterprise application are:

Presentation logic

Business logic

Data Access logic

System Services

Identifying an Enterprise Application (Contd.)

© People Strategists www.peoplestrategists.com Slide 7 of 48

Java Enterprise Edition:

Is Oracle's enterprise Java computing platform.

Is built upon the foundation of Java Platform, Standard Edition (Java SE).

Is an open and standard based platform for developing, deploying, and managing n-tier, Web-enabled, server-centric, and component-based enterprise applications.

It consists of a set of services, APIs, and protocols that provide the functionality for developing multitier, Web-based applications.

Is a collection of related technology specifications that describe required APIs and policies.

Identifying Java Enterprise Edition (EE)

© People Strategists www.peoplestrategists.com Slide 8 of 48

Identifying the Features of Java EE

Java EE comprises the following features:

Client tier support:

J2EE supports pure HTML, as well as Java applets or applications.

It relies on Java Server Pages and servlet code to create HTML or other formatted

data for the client.

Middle Layer support:

J2EE supports Enterprise JavaBeans (EJBs) as middle layer, where the platform's

logic is stored.

Java Database Connectivity (JDBC):

J2EE supports JDBC, which is the Java equivalent to ODBC.

JavaMail API:

It consists of libraries to create, send, receive, and read email messages.

It enables the J2EE applications to handle mail messages easily.

It supports the IMAP4, POP3, and SMTP mail protocols.

© People Strategists www.peoplestrategists.com Slide 9 of 48

Identifying the Features of Java EE (Contd.)

Java Naming and Directory Interface (JNDI) API:

It enables application components and clients to search and link distributed

resources, services, and EJB components.

It allows the configuration of external and custom resources in the Sun ONE

Application Server.

Java Message Service (JMS) API:

It enables the J2EE applications to create, send, receive, and read messages in a

distributed environment.

It uses JMS queues, topics, and message destinations, which are made available

using the JNDI API.

© People Strategists www.peoplestrategists.com Slide 10 of 48

Identifying Various Types of Java EE Container

Various types of Java EE containers are:

Web container: Provides a runtime environment to manage deployed Web

components, such as servlets and JSP.

Applet container: Provides the runtime environment for executing applets.

EJB container: Provides a runtime environment to manage deployed EJB

components.

Application client container: Provides a runtime environment for Java EE

application clients.

The following figure depicts the interaction between different Java EE

containers.

Application client container

Applet container

Web container

EJB container

Database

© People Strategists www.peoplestrategists.com Slide 11 of 48

Identifying JAR, WAR, and EAR

In J2EE application, modules are packaged as Java archive (JAR), Web archive (WAR), and Enterprise archive (EAR) based on their functionality.

JAR:Is a package file format that is used to group Java class files and associated

metadata and resources into one file.

Is used to distribute application software or libraries on the Java platform.

WAR:

Is used to package a Web application, which is a group of JSP, HTML, servlets,

resources and source files.

Includes a Web deployment descriptor file and can be used to import a Web

application into a Web server.

EAR:

Is used by Java EE for packaging one or more modules into a single archive.Is used to enable the deployment of the various modules onto an application simultaneously and coherently.

© People Strategists www.peoplestrategists.com Slide 12 of 48

Identifying Various Java EE APIs and Technologies

Java EE comprises of the following APIs and technologies:

Standard Edition (Java SE)

Java Servlets

Java Server Pages™ (JSP™)

Java Server Faces™ (JSF™)

Remote Method Invocation (RMI)

Java Persistence API (JPA)

Java Message Service (JMS) API

Java Naming and Directory Interface™ (JNDI) API

Java Transaction API (JTA)

Enterprise JavaBeans™ (EJB™) components

Java API for XML Web Services (JAX-WS)

© People Strategists www.peoplestrategists.com Slide 13 of 48

Identifying Java Servlet

A Servlet is a Java program that runs on a server and used to generate dynamic Web pages.

Servlets performs the following tasks:

Processes HTTP requests

Generates dynamic HTTP responses

Servlets act as a middle layer between a request coming from a Web browser or other HTTP client and databases.

Servlets executes inside a Web container as a thread.

The following figure depicts the position of the servlet in a Web application.

HTTP Server

Web BrowserServlet Program

Database

HTTP Protocol

© People Strategists www.peoplestrategists.com Slide 14 of 48

Identifying the Advantages of Java Servlet

Some of the advantages of Java servlets are:

Servlets are faster than CGI programs because each scripts in CGI

produces a new process and these processes takes a lot of time for

execution. However, servlets creates only new thread.

Servlets are server side components that inherits the security

provided by the Web server and Java Security Manager.

Servlets provide all the powerful features of Java, such as exception

handling and garbage collection.

Servlets are portable across operating systems and across Web

servers as servlets are write once, run anywhere (WORA) programs

written in Java.

Servlets can share data among each other and can maintain the

session which helps them to maintain information from request to

request.

The servlet API is designed in such a way that it can be easily

extensible.

© People Strategists www.peoplestrategists.com Slide 15 of 48

Identifying the Limitation of Java Servlet

Some of the limitations of Java servlets are:

Whenever a Web page changes, the servlet needs to be rewritten,

recompiled, and redeployed.

Servlets must handle concurrency issues.

Tedious uses of out.println()statements.

Designing in servlet is difficult and slows down the application.

Writing complex business logic makes the application difficult to

understand.

© People Strategists www.peoplestrategists.com Slide 16 of 48

Identify Servlet Terminology

Basic terminologies used in servlet are:

Hypertext Transfer Protocol (HTTP)

HTTP request

HTTP response

Difference between Web server and application server

Web container

© People Strategists www.peoplestrategists.com Slide 17 of 48

Identifying HTTP Protocol

HTTP is a protocol that is used by a client and a server to communicate with each other.

It supports only one request per connection.

It can be used to transmit any file that follows the Multipurpose Internet Mail Extensions (MIME) specification.

It uses the request and response process to transfer information from one computer to another

© People Strategists www.peoplestrategists.com Slide 18 of 48

Identifying HTTP Request

HTTP request is like an envelope, which contains the necessary information required by the Web server to process a client request.

An HTTP request consists of:

Request line: It contains request method, request URL, and version.

Request header: It contains additional information about the client, such as

the name and the version of the Web browser.

Message body: It is an optional part of the HTTP request. It contains the data

entered by a user that needs processed.

The following figure describes an HTTP request.

© People Strategists www.peoplestrategists.com Slide 19 of 48

Identifying HTTP Response

HTTP response is like an envelope that contains the response generated by a server that would be displayed by a Web browser.

The HTTP response consists of:

Status line: It consists of status code, reason phrase, and version of the

HTTP protocol.

Response header: It contains additional information about the Web server.

Message body: It consists of the response in the form of Hyper Text Markup

Language (HTML) format.

The following figure describes an HTTP response.

© People Strategists www.peoplestrategists.com Slide 20 of 48

Difference Between the Web Server and Application Server

Web Server Application Server

Web server handles the HTTP protocol.

Whenever, a Web server receives an HTTP request, it responds with an HTTP response.

It may respond with a static HTML page or image, send or redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (Java Server Pages), Servlets.

An application server exposes business logic to client applications through various protocols like HTTP, TCP-IP etc.

An application server providers allows the client to access the business logic for use.

An application server is used to run business logic or dynamically generating presentation code.

An application server is more capable of dynamic behavior than web server.

The following points depicts the differences between the Web server and Application server:

© People Strategists www.peoplestrategists.com Slide 21 of 48

Identifying Web Container

What is a Web container?

© People Strategists www.peoplestrategists.com Slide 22 of 48

Identifying Web Container (Contd.)

Web container is a Java virtual machine that supplies an implementation of the Java Servlet API.

All Java servlets must exist in the Web container.

Web Container activate the servlet that match to request.

© People Strategists www.peoplestrategists.com Slide 23 of 48

The following figure depicts how the Web container handles the client request.

Web server

Client

Web container

Servlet

Thread

Service()

doGet()

<html><body>-----------</body></html>

Identifying Web Container (Contd.)

© People Strategists www.peoplestrategists.com Slide 24 of 48

Execution Process of Java Servlets

Following steps illustrates how a Java servlet executes:

1. The client sends request to the server.

2. The Web container, running inside the server, receives the request.

3. The Web container creates a new thread to process the request.

4. The Web container forwards the request to the servlet.

5. The Web container executes the servlet.

© People Strategists www.peoplestrategists.com Slide 25 of 48

Execution Process of Java Servlets (Contd.)

The following figure depicts the execution process of a servlet with a single request.

© People Strategists www.peoplestrategists.com Slide 26 of 48

Execution Process of Java Servlets (Contd.)

The following figure depicts the execution process of a servlet with multiple requests.

Processor’s load does not increase much on execution of multiple requests.

© People Strategists www.peoplestrategists.com Slide 27 of 48

Exploring Servlet API (Contd.)

Servlet API:

is used to create and manage servlets.

consists of various interfaces, classes, and methods.

classes and methods are available in the following packages:

javax.servlet

javax.servlet.http

© People Strategists www.peoplestrategists.com Slide 28 of 48

Exploring Servlet API (Contd.)

The following figure depicts the class hierarchy of the Servlet API.

Servlet API

ServletException

Javax.servlet.http

Servlet

ServletConfig

ServletContext

ServletRequest

ServletResponse

AsyncContext

Javax.servlet

GenericServlet

Cookie

HttpServletRequest

HttpServletResponse

HttpSession

HttpServlet

Packages

Classes

Interfaces

RequestDispatcher

© People Strategists www.peoplestrategists.com Slide 29 of 48

The javax.servlet package:

consists of the following interfaces, as described in the table.

Interface Description

Servlet It provides the methods that all servlets must implement.

ServletConfig It provides information to the servlets during the servlet initialization.

ServletContext It provides methods, which is used by the servlets to communicate with the Web container.

ServletRequest It provides the client request information to the servlet.

ServletResponse It helps the servlet in sending the response to the client.

RequestDispatcher It receives a request from a client and sends it to any other resource, such as a servlet, an HTML page, or a JSP page.

AsyncContext It provides the methods to handle multiple client requests asynchronously.

Exploring Servlet API (Contd.)

© People Strategists www.peoplestrategists.com Slide 30 of 48

The Servlet interface methods are implemented by the servlet class that extends from a GenericServlet or HttpServlet class.

The following table describes the various methods defined in the Servlet interface:

Exploring Servlet API (Contd.)

Method Description

void init(ServletConfig config)

throws ServletExceptionIt is called by the servlet container to indicate to a servlet that the servlet is being placed into service.

ServletConfig getServletConfig() Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.

String getServletInfo() Returns information about the servlet, such as author, version, and copyright.

void service(ServletRequest request,

ServletResponse response) throws

ServletException, IOException

It is Called by the servlet container to allow the servlet to respond to a request.

void destroy() Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

© People Strategists www.peoplestrategists.com Slide 31 of 48

The following table describes the various methods defined in the ServletRequest interface:

Exploring Servlet API (Contd.)

Method Description

public String getParameter(String

paramName)Returns a String object that specifies the value of a particular request parameter.

public String[]

getParameterValues(String paramName)Returns an array of String objects that contains all the values of the request parameter.

public Enumeration

getParameterNames()Returns an Enumeration object containing all the parameter names as String objects that `request contains.

public String getRemoteHost() Returns a String object that specifies the full-qualified name of the computer from which the request is sent.

public String getRemoteAddr() Returns a String object that specifies the IP address of the computer from which the request is sent.

© People Strategists www.peoplestrategists.com Slide 32 of 48

The following table describes the various methods defined in the ServletResponse interface:

Exploring Servlet API (Contd.)

Method Description

public ServletOutputStream

getOutputStream() throws

IOException

Returns an object of the ServletOutputStream class that represents an output stream to send binary data as response.

public PrintWriter getWriter()

throws IOExceptionReturns an object of the PrintWriterclass that the servlet uses to send character data as response.

public void setContentType(String

type)Sets the MIME type for a servlet response. Some of the MIME types are text/plain, image/jpeg, and text/html.

© People Strategists www.peoplestrategists.com Slide 33 of 48

The javax.servlet package:

Consists of the following classes:

GenericServlet

ServletException

The GenericServlet class defines a generic, protocol-independent

servlet.

The GenericServlet implements the Servlet and ServletConfig

interfaces .

The ServletException class defines a general exception a servlet can

throw when it encounters difficulty.

The ServletException class contains the following constructors:

ServletException()

ServletException(String message)

ServletException(String message, Throwable rootCause)

Exploring Servlet API (Contd.)

© People Strategists www.peoplestrategists.com Slide 34 of 48

The following code illustrates a simple example of servlet by inheriting the GenericServlet class:

Exploring Servlet API (Contd.)

import java.io.*;

import javax.servlet.*;

public class myFirstServlet extends GenericServlet{

public void service(ServletRequest req,

ServletResponse res)throws IOException,

ServletException {

res.setContentType("text/html");

PrintWriter out=res.getWriter();

out.print("<html><body>");

out.print("<b>hello generic servlet</b>");

out.print("</body></html>");

}

}

© People Strategists www.peoplestrategists.com Slide 35 of 48

Exploring Servlet API (Contd.)

The javax.servlet.http package:

Provides classes and interfaces that enable you to create a servlet that

communicates using the HTTP protocol.

Provides the HttpServlet class to create servlets, which is an abstract class.

Provides the HttpServlet class that extends the GenericServlet class

and implements Serializable interface.

Provides HTTP specific methods, such as doGet, doPost, and doHead.

Some of the interfaces of the javax.servlet.http package are:`

HttpServletRequest

HttpServletResponse

HttpSession

© People Strategists www.peoplestrategists.com Slide 36 of 48

The following table describes the various methods defined in the HttpServletRequest interface:

Exploring Servlet API (Contd.)

Method Description

public String getHeader(String

fieldname)Returns the value of the request header field, such as Cache-Control and Accept-Language, specified in the parameter.

public Enumeration

getHeaders(String sname)Returns all the values associated with a specific request header as an enumeration of String objects.

public Enumeration

getHeaderNames()Returns the names of all the request headers that a servlet can access as an enumeration of String objects.

© People Strategists www.peoplestrategists.com Slide 37 of 48

The following table describes the various methods defined in the HttpServletResponse interface:

Exploring Servlet API (Contd.)

Method Description

void setHeader(String hname, String

hvalue)Sets the value of header hname to hvalue. If the header has already been set, the new value overwrites the existing value.

void setIntHeader(String hname, int

hvalue)Sets the value of the header hname to the intvalue, hvalue. If the header has already been set, the new value overwrites the existing value.

void addHeader(String hname, String

hvalue)Adds a header hname with value hvalue. This method adds a new header if the header already exists.

void addIntHeader(String hname, int

hvalue)Adds a header hname with an int value, hvalue.

boolean containsHeader(String hname) Returns true if the header hname has already been set with a specific value and false, if not.

void sendRedirect(String url) Redirects a request to the specified URL.

© People Strategists www.peoplestrategists.com Slide 38 of 48

The following table describes the various methods defined in the HttpSession interface:

Exploring Servlet API (Contd.)

Method Description

public void setAttribute(String name,

Object value)Binds the object with a name and stores the name/value pair as an attribute of the HttpSession object. If an attribute already exists, this method replaces the existing attribute.

public Object getAttribute(String

name)Retrieves the String object specified in the parameter, from the session object. If no object is found for the specified attribute, the getAttribute()method returns null.

public Enumeration

getAttributeNames()Returns an Enumeration object that contains the name of all the objects that are bound as attributes to the session object.

© People Strategists www.peoplestrategists.com Slide 39 of 48

The following code illustrates a simple example of servlet by inheriting the HttpServlet class:

Exploring Servlet API (Contd.)

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class myFirstHttpServlet extends HttpServlet

{

public void doGe(HttpServletRequest request,

HttpServletResponse response)throws IOException{

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<body>");

out.println("<h1>Hello Servlet Get</h1>");

out.println("</body>");

out.println("</html>");

}

}

© People Strategists www.peoplestrategists.com Slide 40 of 48

Identifying Servlet Life Cycle

Process the request- service()

Loading servlet, Instantiate and Initialization - init()

Unload servlets / Take out ofservice - destroy()

doGet() or doPost()

Calls

Calls

Calls

The Web container maintains the life cycle of a servlet instance.

The following figure depicts the life cycle of Java servlet.

1. Servlet class is loaded.

2. Servlet instance is created.

3. init()method is invoked.

4. service()method is invoked.

5. destroy()method is invoked.

© People Strategists www.peoplestrategists.com Slide 41 of 48

Implementing a Servlet

To create a servlet, you need to perform the following tasks:

Create a servlet.

Configure the servlet.

Compile and deploy the servlet.

Access the servlet using a browser application.

© People Strategists www.peoplestrategists.com Slide 42 of 48

Creating a Servlet

Servlet can be created by extending the HttpServlet or GenericServlet class.

Example:

Consider a scenario in which you have to create servlet that displays

the current date to the user, each time the servlet is accessed. To do

this, you have to create a servlet that extends from the HttpServlet

class, as shown in the embedded file.

© People Strategists www.peoplestrategists.com Slide 43 of 48

Configuring a Servlet

Configuring a servlet means, associating a URL with the servlet.

Servlets can be configured using either of the way:

Deployment descriptor (DD)

Annotations

A deployment descriptor is an XML file with the name web.xml that

contains configuration information of a Web application.

Each Web application contains one web.xml file.

The elements of the web.xml file enable you to specify:

Servlet context initialization parameters of a Web application.

Initialization parameter for the servlet.

Mapping between a file extension and a MIME type.

Mapping between a servlet and a URL pattern.

The name of the listener class that responds to a servlet life cycle event.

© People Strategists www.peoplestrategists.com Slide 44 of 48

Configuring a Servlet (Contd.)

Some of the commonly used elements of the web.xml file along with their usage are:

<servlet>: specifies the configuration details of all the servlets used in a

Web application and contains the following sub elements:

<servlet-name>

<servlet-class>

<servlet-mapping>: consists of following sub elements:

<servlet-name>

<url-pattern>

<session-config>

<init-param>

<mime-mapping>

Sample DD file:

© People Strategists www.peoplestrategists.com Slide 45 of 48

Configuring a Servlet (Contd.)

Configuring a servlet by using annotations:

The javax.servlet.annotation package defines the @WebServerannotation that you can use to configure a servlet, as shown in the following sample code snippet:

@WebServer(name="MyFirstServlet",

urlPatterns={"/MyFirstServlet", "/MyServlet/*"})

public class MyFirstServlet extends HttpServlet

{

}

The @WebServer annotation is used to configure the MyFirstServletservlet. The name attribute specifies the name of the servlet. The urlPatterns attribute is used to map the servlet with the URL patterns.

© People Strategists www.peoplestrategists.com Slide 46 of 48

Compile and Deploy the Servlet on Server

To You can compile and deploy your application using your Eclipse IDE.

Finally, you can access the Web application using the URL, as shown the following code snippet:

Http://Localhost:8080/myApplication/

© People Strategists www.peoplestrategists.com Slide 47 of 48

Summary

A Web page is an electronic document, which is created using HTML and is translated by the Web browser.

A website is a collection of one or more Web pages, which are hosted on a Web server and are accessed by using client applications, such as a Web browser.

A Web application is a program that is stored on a remote server and delivered over the Internet through a Web browser using various protocols, such as HTTP.

An enterprise application is the term used to describe applications or software that a business would use to assist the organization in solving enterprise problems.

Java EE is built upon the foundation of Java Platform, Standard Edition (Java SE).

Web container provides a runtime environment to manage deployed Web components, such as servlets and JSP.

© People Strategists www.peoplestrategists.com Slide 48 of 48

Summary (Contd.)

Applet container provides the runtime environment for executing applets.

EJB container provides a runtime environment to manage deployed EJB components.

Application client container provides a runtime environment for Java EE application clients.

Java Servlet API consists of two packages:

javax.servlet package

javax.servlet.http package

The various interfaces of javax.servlet package are:

ServletRequest interface

ServletContext interface

ServletResponse interface

© People Strategists www.peoplestrategists.com Slide 49 of 48

Summary (Contd.)

Servlet interface provides the methods that all servlets must implement.

The GenericServlet class defines a generic, protocol-independent servlet.

The GenericServlet implements the Servlet and ServletConfig interfaces .

The ServletException class defines a general exception a servlet can throw when it encounters difficulty.

The HttpServlet class is used to create a servlets that communicates using the HTTP protocol.

The HttpServlet class that extends the GenericServlet class and implements Serializable interface.