Web Applications

To create a Web-based user interface, you need to apply Web-centric components. J2EE provides three types of Web-centric components:

  • JavaServer Pages (JSP)

  • Servlets

  • Web Services

The first two of these components (JSPs and servlets) provide services to clients that use HTTP as a means of communication. For example, JSPs and servlets can interact with the following clients:

  • Standard HTML browsers, such as Microsoft Internet Explorer and Netscape Navigator

  • Java 2 Micro Edition (J2ME) enabled devices, connecting across a wireless network

  • Any Java application using java.net.URL objects

  • Wireless Markup Language (WML) browsers, such as those found on WAP-enabled mobile phones

Web Services expose application or business logic to a client conforming to industry-defined standard protocols using HTTP and XML as a data exchange medium. Web Services are designed to be technology neutral so that J2EE Web Services can be used by non-Java clients such as Microsoft's .NET. Similarly, J2EE clients can use Web Services provided by non J2EE servers, again with .NET being the obvious alternative.

Servlets

Servlets are Java objects that receive incoming client HTTP requests and generate responses for return to the client (typically a browser). Therefore their primary use is to create dynamic Web pages (pages whose content is dependent on variable data).

Servlets are used to implement application or business logic. Servlets often use supporting EJBS for additional application or business logic. However, servlets can be used without EJBs, in which case the servlet provides the application and business logic and JDBC is used to access the data layer. Servlets are discussed further on Day 12, “Servlets.”

Servlets are developed in Java and producing complex HTML documents from within a Java program is a tedious process; JavaServer Pages were introduced to make this much easier while still providing dynamic output.

JavaServer Pages (JSP)

JSPs allow you to dynamically create pages of markup, such as HTML, in response to a client's request. In this respect JSPs are similar to Microsoft's Active Server Pages (ASP).

A JSP consists of a combination of JSP tags, an expression language (EL) and scriplets, which contain the executable code, and static markup, such as HTML. You can think of JSPs as Web pages with embedded Java code. The code contained in the JSP is identified and executed by the server, combined with the static HTML and the resulting page is delivered to the client. This means that the embedded code can generate additional markup dynamically that is delivered to the client alongside the original static markup. The client sees none of this processing, just the resulting HTML.

Servlets and JSPs can both manipulate Java objects and generate HTML pages but each have problems trying to do both jobs. The usual approach for any non-trivial Web Application is to combine servlets and JSPs to take advantage of the strengths of each technology.

Web Application Architecture

Using a single servlet or JSP to process an HTTP request is known as a Model 1 architecture and is shown in Figure 2.2.

Figure 2.2. Model 1 Web Application.


A Model 1 architecture is really only suitable for trivial web applications; it becomes unwieldy when dealing with real world applications.

The Model 2 architecture shown In Figure 2.3 is considered the best approach to developing larger Web Applications.

Figure 2.3. Model 2 Web Application.


In a Model 2 architecture the incoming HTTP request is handled by a servlet which validates any HTTP request data and updates the database if necessary. Typically Session beans will be used to support the servlet functionality. The Session beans in turn may use Entity beans to encapsulate access to a database. In a simple application, one not making use of EJB technology, simple JavaBean helper classes will be used in place of the EJBs.

Once the servlet has verified the request, and obtained the necessary data to formulate the response, the request is forwarded to a JSP for generating the Web page to return to the client. Typically the JSP will only read data from the Session bean so that the different roles of the servlet as a controller and the JSP as a presentation (or view) component are clearly encapsulated. Experienced developers will recognize this architecture as the Model View Controller (MVC) architecture; the EJBs take the role of the Model component, the JSP is the View and the servlet the Controller.

The Model 2 architecture is so useful that products such as Apache Struts (http://jakarta.apache.org/struts) and Sun Microsystems' JavaServer Faces (JSF from http://java.sun.com/j2ee/javaserverfaces) provide frameworks for building Model 2-based Web applications.

Web interfaces are so pervasive in modern enterprise computing that a new technology called Web Services has come to the fore for providing IT services utilizing the Web (HTTP) technologies.

Web Services

Web Services are XML-based middleware components that applications access over HTTP and Simple Object Access Protocol (SOAP). Web Services enjoy industry-wide support and are not a proprietary solution. In fact, because Web Services use XML and open communication standards, any client that can understand SOAP messages can consume Web Services. J2EE provides a rich framework that facilitates the building, deployment, and consumption of Web Services.

In order to send a message to a remote system, something on that system must be listening for incoming messages. An address to which a client can send a Web Service message is termed a Web Service endpoint. The endpoint is described using the Web Service Description Language (WSDL), which tells a client about the location of the endpoint and describes the service that can be found there. The location of a Web Service endpoint is defined as a combination of server name or IP address and a port number on the server at which the Web Service can be found. The description of the Web Service endpoint will define the messages that can be sent to the service, the expected responses, and the protocols over which such messages can be passed (typically SOAP, but other protocol bindings can also be defined).

Web Services are discussed further on Day 20, “Using RPC-Style Web Services with J2EE,” and Day 21, “Message-Style Web Services and Web Service Registries.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45