Java Web Technologies
Java web technologies continually evolve to provide developers with higher levels of ab-straction and greater separation of the application’s tiers. This separation makes web ap-plications more maintainable and extensible. It also allows for an effective division of labor. A graphic designer can build the application’s user interface without concern for the underlying page logic, which will be handled by a programmer. Meanwhile, the program-mer is free to focus on the application’s business logic, leaving the details of building an attractive and easy-to-use application to the designer. Netbeans is the latest step in this evolution, allowing you to develop a web application’s GUI in a drag-and-drop design tool, while handling the business logic in separate Java classes.
Java multitier applications are typically implemented using the features of Java Enter-prise Edition (Java EE). The technologies we use to develop web applications in Chapters 26–27 are part of Java EE 5 (java.sun.com/javaee).
Servlets are the lowest-level view of web development technologies in Java that we will dis-cuss in this chapter. They use the HTTP request/response model of communication be-tween client and server.
Servlets extend a server’s functionality by allowing it to generate dynamic content. For instance, servlets can dynamically generate custom XHTML documents, help provide secure access to a website, interact with databases on behalf of a client and maintain unique session information for each client. A web server component called the servlet container executes and interacts with servlets. Packages javax.servlet and javax.servlet.http provide the classes and interfaces to define servlets. The servlet container receives HTTP requests from a client and directs each request to the appropriate servlet. The servlet pro-cesses the request and returns an appropriate response to the client—usually in the form of an XHTML or XML (Extensible Markup Language) document to display in the browser. XML is a language used to exchange structured data on the web.
Architecturally, all servlets must implement the Servlet interface of package javax.servlet, which ensures that each servlet can execute in the framework provided by the servlet container. Interface Servlet declares methods used by the servlet container to manage the servlet’s life cycle. A servlet’s life cycle begins when the servlet container loads it into memory—usually in response to the first request for the servlet. Before the servlet can handle that request, the container invokes the servlet’s init method, which is called only once during a servlet’s life cycle to initialize the servlet. After init completes execu-tion, the servlet is ready to respond to its first request. All requests are handled by a servlet’s service method, which is the key method in defining a servlet’s functionality. The ser-vice method receives the request, processes it and sends a response to the client. During a servlet’s life cycle, service is called once per request. Each new request is typically han-dled in a separate thread of execution (managed by the servlet container), so each servlet must be thread safe. When the servlet container terminates the servlet (e.g. when the servlet container needs more memory or when it is shut down), the servlet’s destroy method is called to release any resources held by the servlet.
2. JavaServer Pages
JavaServer Pages (JSP) technology is an extension of servlet technology. Each JSP is trans-lated by the JSP container into a servlet. Unlike servlets, JSPs help you separate presenta-tion from content. JavaServer Pages enable web application programmers to create dynamic content by reusing predefined components and by interacting with components using server-side scripting. JSP programmers can use special software components called JavaBeans and custom tag libraries that encapsulate complex, dynamic functionality. A JavaBean is a reusable component that follows certain conventions for class design. For example, JavaBeans classes that allow reading and writing of instance variables must pro-vide appropriate get and set methods. The complete set of class design conventions is discussed in the JavaBeans specification (java.sun.com/products/javabeans/glasgow/ index.html).
Custom Tag Libraries
Custom tag libraries are a powerful feature of JSP that allows Java developers to hide code for database access and other complex operations in custom tags. To use such capabilities, you simply add the custom tags to the page. This simplicity enables web-page designers who are not familiar with Java to enhance web pages with powerful dynamic content and dynamic processing capabilities. The JSP classes and interfaces are located in the packages javax.servlet.jsp and javax.servlet.jsp.tagext.
There are four key components to JSPs—directives, actions, scripting elements and tag li-braries. Directives are messages to the JSP container—the web server component that ex-ecutes JSPs. Directives enable you to specify page settings, to include content from other resources and to specify custom tag libraries for use in JSPs. Actions encapsulate function-ality in predefined tags that programmers can embed in JSPs. Actions often are performed based on the information sent to the server as part of a particular client request. They also can create Java objects for use in JSPs. Scripting elements enable you to insert Java code that interacts with components in a JSP (and possibly other web application components) to perform request processing. Tag libraries are part of the tag extension mechanism that enables programmers to create custom tags. Such tags enable web-page designers to ma-nipulate JSP content without prior Java knowledge. The JavaServer Pages Standard Tag Library (JSTL) provides the functionality for many common web application tasks, such as iterating over a collection of objects and executing SQL statements.
JSPs can contain other static content. For example, JSPs normally include XHTML or XML markup. Such markup is known as fixed-template data or fixed-template text. Any literal text or XHTML markup in a JSP is translated to a String literal in the servlet rep-resentation of the JSP.
Processing a JSP Request
When a JSP-enabled server receives the first request for a JSP, the JSP container translates the JSP into a servlet that handles the current request and future requests to the JSP. JSPs thus rely on the same request/response mechanism as servlets to process requests from and send responses to clients.
3. JavaServer Faces
JavaServer Faces (JSF) is a web application framework (similar to ASP.NET) that simpli-fies the design of an application’s user interface and further separates a web application’s presentation from its business logic. A framework simplifies application development by providing libraries and sometimes software tools to help you organize and build your ap-plications. Though the JSF framework can use many technologies to define the pages in web applications, this chapter focuses on JSF applications that use JavaServer Pages. JSF provides a set of user interface components, or JSF components, that simplify web-page design. These components are similar to the Swing components used to build GUI appli-cations. JSF provides two JSP custom tag libraries for adding these components to a JSP page. JSF also includes APIs for handling component events (such as processing compo-nent state changes and validating user input), navigating between web application pages and more. You design the look-and-feel of a page with JSF by adding tags to a JSP file and manipulating their attributes. You define the page’s behavior separately in a related Java source-code file.
Though the standard JSF components are sufficient for most basic web applications, you can also write custom component libraries. Additional component libraries are avail-able through the Java BluePrints project—which shows best practices for developing Java applications. Many other vendors provide JSF component libraries. For example, Oracle provides almost 100 components in its ADF Faces library. In the next chapter, we discuss one such component library, the BluePrints AJAX components library, which can be found at blueprints.dev.java.net/ajaxcomponents.html.
4. Web Technologies in Netbeans
Every JSP file created in Netbeans represents a web page and has a corresponding JavaBean class called the page bean. A JavaBean class must have a default (or no-argument) constructor, and get and set methods for all of the bean’s properties (i.e., instance vari-ables). The page bean defines properties for each of the page’s elements. The page bean also contains event handlers and page life-cycle methods for managing tasks such as page initialization and rendering, and other supporting code for the web application.
Every Netbeans web application has three other JavaBeans. The RequestBean object is maintained in request scope—this object exists only for an HTTP request’s duration. A SessionBean object has session scope—the object exists throughout a user’s browsing ses-sion or until the session times out. There is a unique SessionBean object for each user. Finally, the ApplicationBean object has application scope—this object is shared by all instances of an application and exists as long as the application remains deployed on a web server. This object is used for application-wide data storage or processing; only one instance exists for the application, regardless of the number of open sessions.
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.