java ee 01-servlets and containers

13

Click here to load reader

Upload: fernando-gil

Post on 13-May-2015

2.459 views

Category:

Education


3 download

DESCRIPTION

Introduction to Java EE technology - Servlet - Servlet Container - Servlet API

TRANSCRIPT

Page 1: Java EE 01-Servlets and Containers

Introduction to Introduction to Java Enterprise EditionJava Enterprise Edition

● First steps with Servlet Technology.●Servlet Container & Application Server

Fernando [email protected]

Marzo 2012

Fernando [email protected]

Marzo 2012

Page 2: Java EE 01-Servlets and Containers

Introduction

● The goal of this course is to explain you how can you start to develop Java EE applications.

● We will accomplished our goals by presenting the theory behind the concepts of Servlets and JavaServer Pages.

● Using JBoss AS or similar we'll present practical exercises to support all the knowledge obtained.

Page 3: Java EE 01-Servlets and Containers

A Servlet is...

● A server-side entity.● A Java programming language class used to extend the

capabilities of servers that host applications access via a request-response programming model.

● The Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET.

Page 4: Java EE 01-Servlets and Containers

Server Responsibilities

● Every server has two main responsibilities:

● Handle Client Requests.

● Create a response to the clients. In the case of HTTP servers that host web applications this could be a difficult task because they need to create dynamic web contents, which may include complicated tasks such as retrieving information from the database, apply business rules and present the information in specific views. The best way to perform this responsibility is create server extensions that we know as Servlets.

Page 5: Java EE 01-Servlets and Containers

Servlet Container

● To deploy and run a servlet, a Web server uses a separate module. This specialized module is called Web container or servlet container and is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights.

● The next picture shows how different components fit together. The file system stores HTML files, the Servlet container execute the Servlets, and business data is in the database. The Web browser sends requests to the Web server. If the target is an HTML file, the server handles it directly but If the target is a servlet then the server delegates the request to the servlet container, once there, the servlet can use the file system and database to generate dynamic output.

Page 6: Java EE 01-Servlets and Containers

All pieces together

Page 7: Java EE 01-Servlets and Containers

Servlet container types

Conceptually, a servlet container is a part of the web server, even though it may run in a separate process. We can classify the servlet container in three types:

● Standalone: Are typically Java-based servers where the servlet container and the web server are integral part of a single program. One common example is Tomcat running by itself.

● In-process: The servlet container is separated from the web server, because is a different program, but runs within the address space of the main server as a plug-in. One example is Tomcat running inside JBoss.

● Out-of-process: The servlet container and the web server are different programs and both run in a different process. To perform communications between them, the web server uses a plug-in usually provided by the servlet container vendor.

Page 8: Java EE 01-Servlets and Containers
Page 9: Java EE 01-Servlets and Containers

Relationship between Servlet Container & Servlet API

● Java's Servlet specification provides a standard and platform-independent framework for communication between servlets and their containers. All the servlet containers must provide this API.

● This framework is made up of a set of interfaces an classes which are collectively called the Servlet Application Programming Interfaces.

● The Servlet API is divided into two packages: javax.servlet and javax.servlet.http

Page 10: Java EE 01-Servlets and Containers

javax.servlet

● This package contains the generic interfaces and classes that are independent of any protocol.

Name Brief Description

Servlet interface This is the central interface in the API. Every class must directly or indirectly implements this interface.

GenericServlet class

It is an abstract class that provides implementation for all the methods except the service() method of the Servlet interface

ServletRequest interface

Provides a generic view of the request that was sent by a client and defines methods that extract information form the request.

ServletResponse interface

Provides a generic way of sending responses to the client.

Page 11: Java EE 01-Servlets and Containers

javax.servlet.http

● This package contains the basic functionality required for HTTP servlets. Interfaces an classes in this package extend the corresponding interfaces an classes of the javax.servlet package.

Name Brief Description

HttpServlet class Is an abstract class that extends GenericSevlet. It adds a new service() method.

HttpServletRequest interface

Extends ServletRequest and provides an HTTP-specific view of the request. It defines methods that extract information such as HTTP headers an cookies from the request.

HttpServletResponse interface

Extends ServletResponse and provides an HTTP-specific way of sending responses. It defines methods that assist in setting information, such as HTTP headers and cookies into the response.

Page 12: Java EE 01-Servlets and Containers

Advantages of the Servlet API

Advantage Description

Flexibility If we need to extend the server functionality all we have to do is write a new servlet specific for that requirement without modifying the server itself.

Separation of responsibilities

The server only needs to worry about network and communication, while the servlet interprets and response to every client request.

Java language Java programmers do not need to lear a new language.

Portability We can write a servlet in one container, and deploy it in another.

Page 13: Java EE 01-Servlets and Containers

Disadvantages of the Servlet API

● One disadvantage, rather restriction is that you have to stick to the rules set for the framework to make the container happy.

● Theoretically using the API you can write servlets for almost any kind of protocol but, by the moment, the Servlet specification only demands support to HTTP.