jump to navigation

Core Syntax August 22, 2005

Posted by Coolguy in Servlets/Jsp.

Core Syntax

  • A JSP page defines a JSP page implementation class that implements the semantics of the JSP page.
  • This class implements the javax.servlet.Servlet Interface
  • At request time a request intended for the JSP page is delivered to the JSP page implementation object for processing.
  • HTTP is the default protocol for requests and responses. Additional request/ response protocols may be supported by JSP containers.
  • A JSP container is a system-level entity that provides life-cycle management and runtime support for JSP pages and servlet components.
  • Requests sent to a JSP page are delivered by the JSP container to the appropriate JSP page implementation object.
  • A traditional application domain of the JSP technology is HTML content.
  • An increasingly important application domain for JSP technology is dynamic XML content using formats like XHTML.
  • A JSP container manages two phases of a JSP page’s lifecycle.
  • In the translation phase, the container validates the syntactic correctness of the JSP pages and tagfiles and determines a JSP page implementation class that corresponds to the JSP page.
  • During the translation phase the container locates or creates the JSP page implementation class that corresponds to a given JSP page. This process is determined by the semantics of the JSP page.
  • In the execution phase the container manages one or more instances of this class in response to requests and other events.

Events in JSP Pages

  • A JSP page may indicate how some events are to be handled.
  • init and destroy events can be described in the JSP page.
  • When the first request is delivered to a JSP page, a jspInit() method, if present, will be called to prepare the page.
  • Similarly, a JSP container invokes a JSP’s jspDe-stroy() method to reclaim the resources used by the JSP page at any time when a request is not being serviced.

JSP Configuration

  • JSP pages may be extended with configuration information that is delivered in the JSP configuration portion of the web.xml deployment description of the web application.
  • The JSP configuration information includes interpretation for the tag libraries used in the JSP files and different property information for groups of JSP files.

Naming Conventions for JSP Files

  • By default the extension .jsp means a top-level JSP file.
  • JSP documents, that is, JSP pages that are delivered as XML documents, use the extension .jspx by default.
  • The jsp-property-group element of web.xml can be used to indicate that some group of files, perhaps not using either of the extensions above, are JSP pages, and can also be used to indicate which ones are delivered as XML documents.

Compiling JSP Pages

  • A JSP page may be compiled into its implementation class plus deployment information during development
  • The benefits of this approach include: • Removal of the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.
    • Reduction of the footprint needed to run a JSP container, as the Java compiler is not needed.

Relative URL’s

  • A context-relative path is a path that starts with a slash (/).It is to be interpreted as relative to the application to which the JSP page or tag file belongs.
  • A page relative path is a path that does not start with a slash (/). It is to be interpreted relative to the current JSP page, or the current JSP file or tag file,


  • There are three types of elements:
  • directive elements,
  • scripting elements,
  • action elements.


  • Directives provide global information that is conceptually valid independent of any specific request received by the JSP page.
  • They provide information for the translation phase.
  • Directive elements have a syntax of the form <%@ directive…%>.


  • Actions provide information for the request processing phase.
  • The interpretation of an action may, and often will, depend on the details of the specific request received by the JSP page.
  • An Actions can either be standard, or custom (that is, provided via the portable tag extension mechanism).
  • Action elements follow the syntax of an XML element. They have a start tag including the element name, and may have attributes, an optional body, and a matching end tag, or may be an empty tag, possibly with attributes:

<mytag attr1=”attribute value”…>body</mytag>
<mytag attr1=”attribute value”…/>
<mytag attr1=”attribute value” …></mytag>

Scripting Elements

  • Scripting elements provide “glue” around template text and actions.
  • JSP 2.0 has a simple Expression Language (EL) that can be used to simplify accessing data from different sources.
  • EL expressions use the syntax ${expr};
  • Other language-based types of scripting elements are: declarations, scriptlets, and expressions.
  • Declarations follow the syntax <%! … %>.
  • Scriptlets follow the syntax <% … %>.
  • Expressions follow the syntax<%= … %>.

JSP Documents

  • A JSP Document is a JSP page that is also an XML document.
  • When a JSP document is encountered by the JSP container, it is interpreted as an XML document first and after that as a JSP page.
  • Among the consequences of this are:
    • The document must be well-formed
    • Validation, if indicated
    • Entity resolution will apply, if indicated

Error Handling

  • Errors may occur at translation time or at request time.
  • The translation of a JSP page source into a corresponding JSP page implementation class by a JSP container can occur at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.
  • In all cases, fatal translation failures shall result in the failure of subsequent client requests for the translation target with the appropriate error specification: For HTTP protocols the error status code 500 (Server Error) is returned.
  • During the processing of client requests, errors can occur in either the body of the JSP page implementation class, or in some other code (Java or other implemen-tation programming language) called from the body of the JSP page implementation class.
  • These exceptions may be caught and handled (as appropriate) in the body of the JSP page implementation class.
  • Any uncaught exceptions thrown in the body of the JSP page implementation class result in the forwarding of the client request and uncaught exception to the errorPage URL specified by the JSP page
  • A JSP is considered an Error Page if it sets the page directive’s isErrorPage attribute to true.
  • If a page has isErrorPage set to true, then the “exception” implicit
    scripting language variable of that page is initialized.
  • The variable is set to the value of the javax.servlet.error.exception request attribute value if present


  • There are two types of comments in a JSP page
  • Comments to the JSP page itself, documenting what the page is doing
  • Comments that are intended to appear in the generated document sent to the client.
  • In order to generate comments that appear in the response output stream to the requesting client, the HTML and XML comment syntax is used, as follows:
    <!– comments … –>
  • A JSP comment is of the form
    <%– anything but a closing –%> … –%>


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: