[ Team LiB ] Previous Section Next Section

The JspEngineInfo Class

Occasionally you might want to obtain information about the current version of the JSP engine. You might even have code that adapts itself to various versions of the JSP specification. For example, you might create a JSP that takes full advantage of the features of JSP version 2.0 and another page that runs under JSP 1.2 but doesn't do everything the 2.0 page does. By examining the current version of the JSP engine, you can decide which page to call.

The JspEngineInfo class defines just one method, which returns a string containing the current version number of the JSP engine. The version number is a series of numbers separated by periods, such as 2.0 or The getSpecificationVersion method returns the current version number:

public String getSpecificationVersion()

You must take a roundabout path to get to the JspEngineInfo object. You must get the object from a JspFactory object. The JspFactory class is used internally by the JSP engine and by the generated servlets. Typically a single JspFactory, which is called the default factory, is used by all the JSPs. To get the default factory, just call getDefaultFactory:

JspFactory defaultFactory = JspFactory.getDefaultFactory();

After you have the JspFactory, you can call getEngineInfo to retrieve the JspEngineInfo object:

JspEngineInfo engineInfo = defaultFactory.getEngineInfo();

Listing 8.3 shows a JSP that prints out the version of the JSP specification that the current JSP engine supports.

Listing 8.3 Source Code for PrintEngineInfo.jsp
The current JSP Engine is:
<%= JspFactory.getDefaultFactory().getEngineInfo().
    getSpecificationVersion() %>

Figure 8.3 shows the output from PrintEngineInfo.jsp.

Figure 8.3. You can get the supported JSP version number from the JSP engine.


You Can Use JspFactory


Section JSP.12.1.3 of the Proposed Final Draft (PFD) 3 for JSP 2.0, which describes JspFactory, might be somewhat confusing. JspFactory is used by containers to obtain an implementation of a PageContext. As shown in the example, you can also use it to obtain information about the JSP engine, such as the version of specification the engine supports.

The end of the description for JspFactory warns that "JspFactory objects should not be used by JSP page authors." It is illegal for anyone other than the container to use setDefaultFactory. Page authors should also use the implicit objects provided by the container instead of using methods from JspFactory to attempt to manage them. The only method you might use is getEngineInfo, when it's necessary to make adjustments to the behavior of your application based on the JSP specification version the engine supports. For most cases, the need to do this is rare. You should carefully consider your design if you need to know the version of JSP specification to run your application.

    [ Team LiB ] Previous Section Next Section