What is a translation unit?

JSP page can include the contents of other HTML pages or other JSP files. This is done by using the include directive. When the JSP engine is presented with such a JSP page it is converted to one servlet class and this is called a translation unit. A Translation Unit is a set of pages that will be translated into a single servlet. Things to remember in a translation unit is that page directives affect the whole unit,

  • The PageContext scope makes variables available for that particular translation unit.
  • You can't define the same variable more than once in a single transaltion unit.
  • You can't define the same bean more than once in a single translation unit.

JSP.1.10.1 The page Directive in JSP 2.1 Specification:

A translation unit (JSP source file and any files included via the include directive) can contain more than one instance of the page directive, all the attributes will apply to the complete translation unit (i.e. page directives are position independent). An exception to this position independence is the use of the pageEncoding and contentType attributes in the determination of the page character encoding; for this purpose, they should appear at the beginning of the page (see Section JSP.4.1). There shall be only one occurrence of any attribute/ value pair defined by this directive in a given translation unit, unless the values for the duplicate attributes are identical for all occurrences. The import and pageEncoding attributes are exempt from this rule and can appear multiple times. Multiple uses of the import attribute are cumulative (with ordered set union semantics). The pageEncoding attribute can occur at most once per file (or a translation error will result), and applies only to the file in which it appears. Other such multiple attribute/value (re)definitions result in a fatal translation error if the values do not match.

Consider the following code:

<jsp:useBean id="address" class="AddressBean" scope="session" />
<jsp:useBean id="address" class="AddressBean" scope="session" />
<jsp:getProperty name="address" property="street" />

The above JSP code will give translation-time errors. Because when the above jsp file is translated into java file (servlet), then the java file will have two reference variable for AddressBean with the same identifier 'address' in the same scope which gives the error.

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |