Transcript of "Java EE 01-Servlets and Containers"
Introduction to Java Enterprise Edition ● First steps with Servlet Technology.Servlet Container & Application Server● Fernando Gil Fernando Gil firstname.lastname@example.org email@example.com Marzo 2012 Marzo 2012
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 well present practical exercises to support all the knowledge obtained.
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.
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.
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.
Servlet container typesConceptually, a servlet container is a part of the web server, even thoughit may run in a separate process. We can classify the servlet container inthree 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.
Relationship between Servlet Container & Servlet API● Javas 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
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 It is an abstract class that provides implementation class for all the methods except the service() method of the Servlet interface ServletRequest Provides a generic view of the request that was sent interface by a client and defines methods that extract information form the request. ServletResponse Provides a generic way of sending responses to the interface client.
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 Extends ServletRequest and provides an HTTP- interface specific view of the request. It defines methods that extract information such as HTTP headers an cookies from the request. HttpServletResponse Extends ServletResponse and provides an HTTP- interface specific way of sending responses. It defines methods that assist in setting information, such as HTTP headers and cookies into the response.
Advantages of the Servlet APIAdvantage DescriptionFlexibility 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 The server only needs to worry aboutresponsibilities 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.
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.
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.