[ Team LiB ] Previous Section Next Section

How WebLogic Server Works with Servlets

JSP pages are processed using two distinct phases—translation and request processing—as shown in Figure 15.1.

Figure 15.1. JSP processing.

graphics/15fig01.gif

During the translation phase, which happens only during the initial request for the JSP page (assuming no changes), the JSP page is parsed into a Java file and then compiled into a server-side implementation class. This class implements the JavaSoft Servlet 2.3 interface.

NOTE

Translation of the final version of the JSP page is normally the last step of the development process. Within a production environment, all JSP pages should be precompiled to avoid translation phase latency.

The servlet source code resulting from the parsing process is not retained. However, modifying WebLogic JSP configuration parameters, which is discussed within this chapter, will enable automatic retention of this source code.


The WebLogic JSP implementation class that's produced extends the class weblogic.server.jsp.JspBase and implements the interface weblogic.server.jsp.StaleIndicator. This implementation class is made available to WebLogic Server for execution. At this point, the translation phase ends and the request-processing phase begins with the _jspService() method of the implementation class processing client requests.

Let's implement a simple example to see how JSPs are processed on WebLogic Server. Try implementing JSP code that displays counts from 1 to 5, as shown in Listing 15.1.

Listing 15.1 Counting.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 <html>
 <head>
    <title>Counting</title>
 </head>

<h1>This Counts</h1>
<%
   for (int i=1; i<=5; i++) {
 %>
    <h2>The Count is: <%=i %> </h2>
 <% }
 %>

 </body>
 </html>

Notice how Java code is included within HTML code in Listing 15.1, bold lines 9–14. To view this JSP, start your server and deposit the JSP file into your WebLogic domain's applications\DefaultWebApp directory. Then access the page using a browser, as shown in Figure 15.2.

Figure 15.2. Browser view of Counting.jsp.

graphics/15fig02.gif

TIP

If you're having problems finding the active default Web application directory, launch the WebLogic Examples Server. Within the server start window, a notice is displayed that contains the URL to access the server's index JSP (this page should launch automatically). Deposit your sample JSP into the corresponding directory (the directory containing the index.jsp file should be ..weblogicHome\samples\server\examples\build\examplesWebApp). If you're still lost, an inspection of the corresponding config.xml file (find <Server DefaultWebApp=".. "—this names the default application) will reveal the absolute path of the default Web application.


TIP

The URL displayed in the example is http://IP(machine_name):port/examplesWebApp/counting.jsp try also http://IP(machine_name):port/counting.jsp.

The config.xml server tag's DefaultWebApp attribute (that is, <Server DefaultWebApp="..."</Server>) controls this behavior. No preface is needed (as with the second option shown earlier) if an application is designated as DefaultWebApp within the server tag.


Notice how the Java-implemented logic has been executed and displayed using HTML format. This is an example of the power and utility that the JSP paradigm gives developers. Behind the scenes, you'll also find that the servlet class, counting.class, has been created and deposited on your server. The generated Java source file, __counting.java, is displayed in Listing 15.2.

Listing 15.2 _counting.java
/* compiled from JSP: /counting.jsp
*
* This code was automatically generated at 8:19:24 AM on Apr 30, 2003
* by weblogic.servlet.jsp.Jsp2Java -- do not edit.
*/

package jsp_servlet;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

// User imports

// built-in init parameters:
// boolean       _verbose -- wants debugging

// Well-known variables:
// JspWriter out         -- to write to the browser
// HttpServletRequest request  -- the request object.
// HttpServletResponse response  -- the response object.
// PageContext pageContext    -- the page context for this JSP
// HttpSession session      -- the session object for the client (if any)
// ServletContext application   -- The servlet (application) context
// ServletConfig config      -- The ServletConfig for this JSP
// Object page          -- the instance of this page's implementation class (i.e., 'this')

/**
* This code was automatically generated at 8:19:24 AM on Apr 30, 2003
* by weblogic.servlet.jsp.Jsp2Java -- do not edit.
*
* Copyright (c) 2003 by BEA Systems, Inc. All Rights Reserved.
*/
public final class __counting
extends
weblogic.servlet.jsp.JspBase
implements weblogic.servlet.jsp.StaleIndicator
{

 // StaleIndicator interface
 public boolean _isStale() {
  weblogic.servlet.jsp.StaleChecker sci =(weblogic.servlet.jsp.StaleChecker)
graphics/ccc.gif(getServletConfig().getServletContext());
  java.io.File f = null;
  long lastModWhenBuilt = 0L;
  if (sci.isResourceStale("/counting.jsp", 1051715956000L, "8.1.0.0")) return true;
  return false;
 }

 public static boolean _staticIsStale(weblogic.servlet.jsp.StaleChecker sci) {
  java.io.File f = null;
  long lastModWhenBuilt = 0L;
  if (sci.isResourceStale("/counting.jsp", 1051715956000L, "8.1.0.0")) return true;
  return false;
 }

 private static void _writeText(ServletResponse rsp, JspWriter out, String block, byte[]
graphics/ccc.gif blockBytes) throws IOException {
  if (!_WL_ENCODED_BYTES_OK || _hasEncodingChanged(rsp)) {
   out.print(block);
  } else {
   ((weblogic.servlet.jsp.ByteWriter)out).write(blockBytes, block);
  }
 }

 private static boolean _hasEncodingChanged(ServletResponse rsp) {
  String encoding = rsp.getCharacterEncoding();
  if ("ISO-8859-1".equals(encoding) || "Cp1252".equals(encoding) || ISO8859_1".equals
graphics/ccc.gif(encoding) || "ASCII".equals(encoding)) {
   return false;
  }
  if (_WL_ORIGINAL_ENCODING.equals(encoding)) {
   return false;
  }
  return true;
 }

 private static boolean _WL_ENCODED_BYTES_OK = true;

 private static final String _WL_ORIGINAL_ENCODING = "Cp1252";

 private static byte[] _getBytes(String block) {
  try {
   return block.getBytes(_WL_ORIGINAL_ENCODING);
  } catch (java.io.UnsupportedEncodingException u) {
   _WL_ENCODED_BYTES_OK = false;
  }
  return null;
 }
 private final static String _wl_block0 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
graphics/ccc.gif Transitional//EN\">\r\n\r\n<html>\r\n<head>\r\n<title>Counting</title>\r\n</head>\r\n\r
graphics/ccc.gif\n<h1>This Counts</h1>\r\n";
 private final static byte[] _wl_block0Bytes = _getBytes(_wl_block0);
 private final static String _wl_block1 = "\r\n   <h2>The Count is: ";
 private final static byte[] _wl_block1Bytes = _getBytes(_wl_block1);
 private final static String _wl_block2 = " </h2>\r\n";
 private final static byte[] _wl_block2Bytes = _getBytes(_wl_block2);
 private final static String _wl_block3 = "\r\n\r\n</body>\r\n</html>\r\n";
 private final static byte[] _wl_block3Bytes = _getBytes(_wl_block3);


 public void _jspService(javax.servlet.http.HttpServletRequest request, javax.servlet.http
graphics/ccc.gif.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
 {

  // declare and set well-known variables:
  javax.servlet.ServletConfig config = getServletConfig();
  javax.servlet.ServletContext application = config.getServletContext();
  javax.servlet.jsp.tagext.Tag _activeTag = null;
  // variables for Tag extension protocol

  Object page = this;
  javax.servlet.jsp.JspWriter out;
  javax.servlet.jsp.PageContext pageContext =
  javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
graphics/ccc.gif null, true, 8192, true);

  out = pageContext.getOut();
  JspWriter _originalOut = out;

  javax.servlet.http.HttpSession session = request.getSession(true);

  try { // error page try block

   _writeText(response, out, _wl_block0, _wl_block0Bytes);
   //[ /counting.jsp; Line: 9]
   for (int i=1; i<=5; i++) { //[ /counting.jsp; Line: 10]
    _writeText(response, out, _wl_block1, _wl_block1Bytes);
    out.print(String.valueOf(i )); //[ /counting.jsp; Line: 12]
    _writeText(response, out, _wl_block2, _wl_block2Bytes);
   } //[ /counting.jsp; Line: 13]
   _writeText(response, out, _wl_block3, _wl_block3Bytes);
  } catch (Throwable __ee) {
   while (out != null && out != _originalOut) out = pageContext.popBody();
   ((weblogic.servlet.jsp.PageContextImpl)pageContext).handlePageException(
graphics/ccc.gif(Throwable)__ee);
  }

  //before final close brace...
 }
}

Comparing the original JSP code in Listing 15.1 to the generated servlet code in Listing 15.2 clearly shows the volume of Java code generated in parsing a single JSP. Note the specific methods implemented within the generated code. Viewing this file is helpful during debug operations. The location of generated files might vary depending on your individual configuration. A file search of your server's root directory will locate the files.

NOTE

The associated Java file is retained only if the JSP parameter "keepgenerated=true" is set. JSP configuration parameters and syntax are discussed later in this chapter. You can inspect these parameters by examining the weblogic.xml file located in the directory ..\applications\DefaultWebApp\WEB-INF.


JSP Compiler

The WebLogic Server automatically compiles your JSP pages upon initial client request. However, WebLogic gives you the option of independently compiling JSP pages. This convenience is useful in development and debugging situations.

The WebLogic JSP compiler (weblogic.jspc) parses your JSP pages into a .java file. This Java file is then compiled to produce the server-side implementation class discussed earlier. The WebLogic JSP compiler is executed as shown here:


$ java weblogic.jspc -keepgenerated filename.jsp

This line keeps the generated .java file for inspection.

NOTE

A compilation warning is issued if the web.xml or weblogic.xml file isn't present. However, the absence of these files does not prevent successful compilation.


Commonly used JSP compile options are included in Table 15.1. See the BEA documentation site at http://edocs.beasys.com/wls/docs81/jsp for detailed JSP compile options.

Table 15.1. Common JSP Compile Options

Option

Description

-compileAll

Compiles all JSP pages within current directory or directory identified with -webapp option.

-compiler <compiler-location>

Specifies compiler to use. Default is javac.

-help

Displays available options.

-k

If compiling multiple JSP pages, continues to compile next JSP page if errors encountered in previous page(s).

-keepgenerated

Keeps generated source code.

-package <name>

Sets package name. Default is jsp_servlet.

-webapp <folder>

Directory containing an exploded Web application. The compiler will search this directory for tag libraries or Java classes used by the compiling class.

    [ Team LiB ] Previous Section Next Section