V3 - 09/18/2009 AL Patrick c01.tex MA TE RI Building Web Applications in WebLogic HT ED Web applications are an important part of the Java Enterprise Edition (Java EE) platform because the Web components are responsible for key client-facing presentation and business logic. A poorly designed web application will ruin the best business-tier components and services.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Servlets Use the Request/Response Model Java servlets are a request/response mechanism: a programming construct designed to respond to a particular request with a dynamic response generated by the servlet’s specific Java implementation. Servlets may be used for many types of request/response scenarios, but they are most often employed in the creation of HyperText Transfer Protocol (HTTP) responses in a web application.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic WebLogic Server provides a number of useful sample servlets showing the basic approach for creating HTTP servlets. These sample servlets are located in the samples/server/examples/ src/examples/webapp/servlets subdirectory beneath the WebLogic Server home directory, a directory we refer to as $WL_HOME throughout the rest of the book.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic class, creates an instance of the class, initializes the instance, calls the servlet’s init() method, and calls the service() method to process the request. In normal servlet operation, this same instance of the Servlet class will be used for all subsequent requests. Servlets may be preloaded during WebLogic Server startup by including the element in the web.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic use. The specification encourages developers to protect critical sections of servlet code using synchronization logic. Of course, using synchronization logic around non-thread-safe code comes with a price — it invariably creates bottlenecks and latency in high volume systems as threads wait for their turn to execute the protected code.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic session ID has the name JSESSIONID by default and consists of a long hash identifying the client plus creation-time and cluster information.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic WebLogic Server supports several forms of session persistence, a mechanism for providing session failover. The two most commonly used forms are in-memory replication and JDBC persistence. When using these types of session persistence, be careful not to place very large objects in the HttpSession. WebLogic Server tracks changes to the session object through calls to the setAttribute() method.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic JSP Pages Are Converted to Servlets The key to understanding JSP pages is to recognize that the JSP file itself is simply the input for a multistep process yielding a servlet. In the key processing step, the JSP page is parsed by the application server and converted to the equivalent pure Java servlet code. All text that is not part of JSP tags and scripting elements is assumed to be part of the HTTP response.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Table 1-2: JSP Syntax Elements Element Syntax Description Scriptlet <% scriptlet code %> Java code placed directly in _jspservice() method at this location. Declaration <%! declaration %> Java code placed within the generated servlet class above the _jspservice() method definition. This usually defines class-level methods and variables.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic direct use of implicit objects in JSP scriptlet code is considered poor form. The JavaServer Pages Standard Tag Library (JSTL), discussed later in this chapter, provides access to data stored within these implicit objects — and many others — in a much safer and more standard way. Table 1-3: JSP Implicit Objects Object Type Description request javax.servlet.http.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Best Practice Disable session tracking in JSP pages that do not require this feature to avoid unnecessary session persistence. Like servlets, JSP pages are normally multithreaded and may process multiple requests simultaneously. The same thread-safety restrictions that apply to servlets also apply to JSP pages unless the JSP is configured to be single threaded.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic When the buffer fills, the response is committed, and the first chunk of information is sent to the browser. Once this commit occurs, the server will no longer honor jsp:forward, HTTP header changes (such as redirects), or additional cookies. The server will generate an IllegalStateException if any of these operations is attempted after the buffer fills and the response is committed.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Web Application Best Practices Now that you have reviewed some of the key concepts related to web applications in WebLogic Server, it’s time to dig in and discuss best practices. So many options are available to designers and developers of Java EE web applications that it would require an entire book to list and explain all of the web application best practices we could conceivably discuss.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Listing 1-1: ErrorPage.jsp. (continued)
Requested URL
<%= HttpUtils.getRequestURL(request) %>
Request Parameters
<% Enumeration params = request.getParameterNames(); while(params.hasMoreElements()){ String key = (String)params.nextElement(); String[] paramValues = request.getParameterValues(key); for(int i = 0; i < paramValues.length; i++) { out.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Accessing the ErrorCreator.jsp page from a browser now causes a useful error message to be displayed to the user. The page could conform to the look and feel of the site itself and could easily include links to retry the failed operation, send an email to someone, or go back to the previous page.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Calling Custom Tags in JSP Pages Custom tags are invoked within JSP pages by embedding the appropriate XML tags in your page using the syntax
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic string within the braces as an expression that should be parsed and treated as a request for data from some source available to the page. By specifying requestScope in the expression, we are indicating that the tag should look only in the HttpServletRequest object, and by specifying employeeNum we are telling the tag which parameter or attribute we want from the request object.
Patrick c01.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic JSTL Contains Five Tag Libraries The JavaServer Pages Standard Tag Library (JSTL) contains the following tag libraries: ❑ The JSTL core library contains tags for common operations such as flow control (conditionals, looping), generating output, setting variables, and creating standard HTML links. Nearly every application makes use of the core library.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Custom Tag Key Concepts Custom tags require a minimum of three components: ❑ The Tag Handler Class is a Java class implementing either the javax.servlet.jsp.tagext.Tag or BodyTag interfaces. The tag handler class defines the behavior of the tag when invoked in the JSP page by providing set methods for attributes and implementations for key methods such as doStartTag() and doEndTag().
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Best Practice The JSP 2.0 tag files mechanism provides a new approach for sharing and including JSP pages and page fragments that might make sense if the older action and <%@include file="..." %> directive do not meet your needs. Be sure there is sufficient value in changing to the new approach since it represents additional complexity.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Table 1-5: Custom Tag Sources Location Description http://jakarta.apache.org/taglibs This source has a number of open source tag libraries, providing everything from string manipulation to regular expression handling to database access. It also hosts an implementation of the JSTL specification. http://jakarta.apache.org/struts Struts is a model-view-controller framework that includes a number of useful tag libraries.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic pages and servlets using either entries in the web.xml descriptor file or annotations in the filter class. To illustrate this process, we will build and deploy a simple but useful filter that intercepts servlet and JSP requests and logs HttpServletRequest information before passing the request on to the intended JSP page or servlet.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic ) public class SnoopFilter implements Filter { ... } Although this is obviously a simple example, SnoopFilter illustrates the value of filters for preprocessing activities such as logging, auditing, or debugging in Java EE web applications.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic Additional initialization parameters for the CacheFilter include the following: Name The name of the cache. It defaults to the request URI. Timeout Timeout period for the cached content. It defaults to seconds, but it may be specified in units of ms (milliseconds), s (seconds), m (minutes), h (hours), or d (days). Scope The scope of the cached content. Valid values are request, session, application, and cluster.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic weblogic.cache.filter.CacheFilter timeout 60 key parameter.howmany ... CacheFilter2 CacheFilterTest2.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic To create a spreadsheet using a servlet, build the servlet in the normal manner but set the content type to application/vnd.ms-excel in the response header to indicate that the response should be interpreted as a spreadsheet. Data written to the response Writer object will be interpreted as spreadsheet data, with tabs indicating column divisions and newline characters indicating row divisions.
Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic SimpleExcelServlet /simpleexcel As noted earlier, WebLogic Server supports a @WLServet annotation for registering servlets and specifying URL patterns. In this case, the SimpleExcelServlet source file could include the following @WLServlet annotation to eliminate the need for web.
Patrick c01.tex V3 - 09/18/2009 12:15pm Chapter 1: Building Web Applications in WebLogic { PrintWriter out = response.getWriter(); response.setContentType(CONTENT_TYPE_EXCEL); out.print("
"); out.print(""); out.print(" | "); // empty cell in upper corner for (int jj = 1; jj <= 10; jj++) { out.print("" + jj + " | "); } out.print("
"); for (int ii = 1; ii <= 10; ii++) { out.print(""); out.Patrick c01.tex V3 - 09/18/2009 Chapter 1: Building Web Applications in WebLogic Viewing Generated Servlet Code Viewing the servlet code generated for a particular JSP page can be instructive while learning JSP technology and useful during the testing and debugging process. Often the error report received during the execution of the JSP page indicates the line in the generated servlet code, but finding the JSP scriptlet code or tag that caused the error requires inspection of the Java code.