Introduction To Java Enterprise Edition
Introduction To Java Enterprise Edition
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.
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
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.
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.
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 javax.servlet and javax.servlet.http two packages:
javax.servlet
This package contains the generic interfaces and classes that are independent of any protocol.
Name
Servlet interface GenericServlet class ServletRequest interface ServletResponse interface
Brief Description
This is the central interface in the API. Every class must directly or indirectly implements this interface. It is an abstract class that provides implementation for all the methods except the service() method of the Servlet interface Provides a generic view of the request that was sent by a client and defines methods that extract information form the request. Provides a generic way of sending responses to the 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 HttpServlet class HttpServletRequest interface Brief Description Is an abstract class that extends GenericSevlet. It adds a new service() method. Extends ServletRequest and provides an HTTPspecific view of the request. It defines methods that extract information such as HTTP headers an cookies from the request. Extends ServletResponse and provides an HTTPspecific way of sending responses. It defines methods that assist in setting information, such as HTTP headers and cookies into the response.
HttpServletResponse interface
Separation of responsibilities
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.