Chapter 24: Creating JavaServer Pages
For JSPs to run in EAServer, they must belong to a Web application. In addition, you can map servlets to JSPs. When the servlet is called, the corresponding JSP is invoked. This section discusses:
EAServer fully supports the features described in the JavaServer Pages 1.2 specification as well as mapping requests to JSPs as described in the Java Servlet 2.3 specification. In EAServer the JSP Engine is implemented as a generic servlet, which is referred to as the JSP servlet. The JSP servlet handles runtime translation and compilation of JSPs, if required, as well as invoking the generated servlet for a given JSP.
The JSP servlet supports translation of JSPs containing JSP standard directives, standard actions, custom tags and scripting elements such as declarations, scriptlets and expressions. For JSPs that include custom JSP tags, a tag handler is loaded every time it is needed. Tag handlers are not pooled. The JSP servlet also supports all the semantics associated with the “extends” attribute.
A Web application is a collection of resources that is mapped to a specific Uniform Resource Identifier (URI) prefix. These resources may include JSPs, servlets, HTML files, and images. The URI that is stored in the request data structure is used to retrieve a JSP. The JSP Servlet creates a unique name for a generated servlet. Generated servlet names are stored in a hash table. For a given request URI, the JSP Servlet determines which generated servlet name it corresponds to. It then looks up the generated servlet name in the hash table; an entry in the hash table indicates that the JSP has been precompiled.
If a JSP is not precompiled, the JSP servlet invokes the compiler and saves the generated files in the appropriate directory. It then executes the page by invoking the _jspService method on the generated servlet.
If a JSP is precompiled, the JSP servlet compares the timestamp of the JSP and all its nested include files, if any, with the timestamp of the generated servlet. If any time stamp of the JSP is more recent than that of the generated servlet, the JSP is recompiled. If the generated servlet is current, the JSP Servlet creates a new instance of the precompiled servlet class and calls _jspService method on it.
The JSP Servlet uses CLASSPATH from ServletConfig for compiling the generated servlet. To change the directory where servlets are generated, set the “scratchdir” parameter as one of the parameters in ServletConfig. “scratchdir” is passed to the “init” method on the JSP Servlet; you can set it by using one of the init-args on the Servlet Properties tab.
The JSP 1.2 specification extends JSP 1.1 in a number of ways:
Uses Servlet 2.3 as the foundation for its semantics.
Defines the XML syntax for JSPs.
Provides for translation-time validation of JSPs.
A new compilation phase has been added that gives custom tag libraries the opportunity to examine an XML view of the parsed page, and throw a translation time exception if problems are detected.
The ability for tag libraries to include event listener classes. The listeners are listed in the tag library descriptor and the JSP container automatically instantiates the listener classes and registers them in a similar way to web.xml. Essentially, the mechanism locates the TLDs in the Web Application (either in WEB-INF/classes or WEB-INF/lib), reads their <listener> elements, and regards them as an extension of those listed in web.xml.
Better specification of the tag handler contract – tag handlers are Java classes that implement specific servlet interfaces. JSP 1.2 introduces some new interfaces and changes some existing interfaces to provide better support.
Improvements on authoring support – extends tag library functionality.
Better I18N support – implements the
to provide support for dynamic content-type.
Fixes the “flush before you include” limitation in JSP 1.1.
For detailed information about JavaServer Pages technology, see the Java software Web site.
When you create a JSP using EAServer Manager, the load during startup option determines if your JSPs are compiled at server start-up or when the JSP is first called. You can also use a command-line utility to compile your JSPs manually. This allows you to debug and test your JSPs without running the server. This section describes the JSP compiler supplied with your EAServer installation.
The %JAGUAR%\bin\jspc.bat (Windows) and $JAGUAR/bin/jspc.sh (UNIX) compiler provides you with several options for compiling your JSPs. This section uses jspc.sh for demonstration purposes. All options are valid for both systems.
You can also compile JSPs with the jagtool or jagant compilejsp command. For details, see Chapter 12, “Using jagtool and jagant,” in the EAServer System Administration Guide.
jspc.sh <options> <jsp files>|<jsp dirs>
-webapp <dir> The name of the Web application directory, relative to the $JAGUAR/Repository/WebApplication directory. For example, MyWebApp identifies the $JAGUAR/Repository/WebApplicationMyWebApp directory.
jsp files include any number of:
This section provides examples of the various jspc compiler options:
jspc.sh -webapp MyWebApp /pets/cat.jsp /pets/dog.jsp
The compiler identifies the $JAGUAR/Repository/WebApplication/MyWebApp directory as the Web application directory and compiles the cat.jsp and dog.jsp files located in the $JAGUAR/Repository/WebApplication/MyWebApp/pets directory.
jspc.sh -uriroot $JAGUAR/Repository/WebApplicaton/MyWebApp -d $JAGUAR/work /pets/cat.jsp /pets/dog.jsp
The compiler identifies the $JAGUAR/Repository/WebApplication/MyWebApp directory as the Web application directory and compiles the cat.jsp and dog.jsp files, which are located in the pets subdirectory, and outputs the results to the $JAGUAR/work directory.
jspc.sh -uriroot $JAGUAR/Repository/WebApplicaton/MyWebApp -d $JAGUAR/work -jspdir $JAGUAR/Repository/WebApplicaton/MyWebApp
The compiler identifies the $JAGUAR/Repository/WebApplication/MyWebApp directory as the Web application directory and compiles all JSP files contained in it and any subdirectories, and outputs the results to the $JAGUAR/work directory.
jspc.sh -uriroot $JAGUAR/Repository/WebApplicaton/MyWebApp -d $JAGUAR/work -keep /pets/dog.jsp /pets/cat.jsp
the -keep option notifies the compiler to keep all generated Java files in the output directory, along with the compiled JSP files.
JSPs are contained within Web applications. JSP source code and class files are stored relative to the Web application to which it belongs.
Normally, EAServer deletes the Java source code after compiling a JSP. To keep the generated source code to view or use in a debugger:
Display the properties for the Web application in which the JSP is installed.
On the Advanced tab, set the property com.sybase.jaguar.webapplication.keepgenerated to true.
When the source code is preserved, you can find it in the same directory as the JSP class files. In the default configuration, EAServer maintains a separate JSP class directory for each server in your EAServer installation:
Where server is the name of your server, and WAName is the name of your Web application.
You can enable sharing of class files by setting the Web application property com.sybase.jaguar.webapplication.sharecompiledjspclasses to true. If sharing is enabled, EAServer compiles and loads JSP classes from the directory:
Where WebAppName is the Web application name.
JSPs in EAServer must be created in a Web application. If necessary, create the Web application to contain the JSPs as described in Chapter 21, “Creating Web Applications.” You can create new JSPs in EAServer Manager or import them from existing JSP source files.
Creating or importing JSPs
In EAServer Manager, select the Web Application folder.
Select an existing Web application to which you are adding a JSP, then import the JSP or create a new one as follows:
EAServer creates the JSP under the Web applications folder. For example, if you name your JSP testjsp and it belongs to the MyWebApp application, then the location is EAServer_home/Repository/WebApplication/MyWebApp/testjsp.
Importing JSP files If you have existing JSPs that you want to add to your Web application or if you create JSPs with another editing tool, you can copy them to this location, making sure the JSP you copy and the name you enter for the JSP match.
Configuring the JSP properties
To configure your JSP, double-click the JSP or highlight the JSP and select File | Web Application Component Properties. Complete the information described below:
General properties – select this tab to enter general parameters for your JSP:
Description – a brief description of the JSP file.
Web Component Type – select JSP.
JSP File Name – the name and path of the JSP file. The path is relative to the Web application context. For example, if you enter /work/test.jsp, the JSP will be placed in EAServer/Repository/WebApplication/appname/work/test.jsp, where EAServer is the EAServer installation directory and appname is the Web application name. The JSP file must include the .jsp extension.
Startup Load Sequence Position – enter a number that indicates when the JSP loads in relation to other JSP files when the Web application starts. This option applies only if Load During Startup is true. The lower the number the earlier it loads; 1 indicates that this JSP loads first. If a JSP is dependent on another JSP that requires time to initialize, specify the JSP that requires additional initialization time to load first. JSPs with a startup load sequence position of 0 loads last.
Load During Startup – this option compiles and translates the JSP into a servlet at start-up. If you do not select this option, the JSP is compiled when it is first called.
Init-Args – select this tab to enter the initialization parameters associated with the JSP. If your JSP uses externally configured parameters, add or modify the values with the controls on this tab:
Add – enter the Initialization Property Name. Add a default value for your parameter in the Property Value window.
Modify – highlight the argument you want to modify and click Modify. Make your modifications and click OK.
Delete – highlight the argument you want to delete and click Delete.
Advanced – to improve JSP performance, set the value of the com.sybase.jaguar.webapplicaton.jspc-interval property, which determines if and when the JSP runtime checks whether a JSP is current. Set the property value to an integer.
If set to a negative number, the JSP runtime never checks.
If set to 0, the JSP runtime always checks.
To specify the number of seconds before the next check, set the value to a number greater than 0. If a request comes in before the time expires, the JSP is not checked.
Complete the rest of the properties as you would for a servlet.
To configure security for your JSP, see Chapter 3, “Using Web Application Security,” the EAServer Security Administration and Programming Guide.
Editing the JSP source
EAServer supplies an editor for creating and modifying your JSP files; however, you can use any text editor to perform the same tasks. To edit the JSP in EAServer Manager:
Open the Web Application folder and select the Web application to which the JSP belongs.
Highlight the JSP.
Select File | Edit JSP. An editor displays where you can view and modify your JSP. Locate other files for editing by selecting File | Open. When you are finished, select File | Save.
Deleting a JSP
Open the Web Application folder and select the Web application to which the JSP file belongs.
Highlight the JSP.
Select File | Delete Web Application Component.
EAServer supports international versions of your Web application resources: Servlets, static Web pages, and so on. For more information, see “Localizing Web applications”.
EAServer supports path mappings as described in the Java Servlet 2.3 specification. Mappings are defined at the Web application level. Refer to Chapter 21, “Creating Web Applications” for information about Request path mappings.
EAServer supports page caching, which improves the performance of servlet and JSP requests. When page caching is enabled for a servlet or JSP Web component, the cache is checked before invoking the Web component. For more information, see “Dynamic page caching” in Chapter 5, “Web Application Tuning,” in the EAServer Performance and Tuning Guide.
EAServer supports servlet filters as described in the Java Servlet 2.3 specification. Filters are defined at the Web application-level. For information on creating filters, see Chapter 23, “Using Filters and Event Listeners.”
|Copyright © 2005. Sybase Inc. All rights reserved.|