NonStop Servlets for JSP System Administrator's Guide
Overview and Architecture
NonStop Servlets for JavaServer Pages (NSJSP) System Administrator’s Guide—525644-001
1-9
JSP Syntax Basics
1. Obtain a reference to the servlet context by invoking getServletContext().
The return value is a javax.servlet.ServletContext object, which contains
several methods for the servlet to communicate with the container. The
getServletContext() method is invoked on the ServletConfig object
passed in to a servlet’s init() method and is typically stored as a field in the
servlet class.
2. Obtain a reference to the request dispatcher object for the servlet in use. Use the
getRequestDispatcher(String name) method, which takes in the path and
name of the servlet and returns the request dispatcher object.
3. Invoke either the include or forward method of the RequestDispatcher object.
Either method takes two arguments; the HttpServletRequest and
HttpServletResponse objects.
Sample code for a simple JSP include directive is shown in An Example of JSP Code
on page 1-10. You will see that JSP is a lot easier to code than directly coding the
servlets in native Java, as shown above.
JSP Syntax Basics
Syntax has two main classifications: directives and scripting elements such as
declarations, expressions and scriptlets.
Directives
The Page Directive. This directive is a JSP tag that you will use in almost every JSP
source file you write. The page directive gives instructions to the JSP container that
apply to the entire JSP source file. For example, a page directive can include
comments that will become part of the compiled JSP file or the scripting language used
in the JSP source file, packages the source file would import, or the error page called if
an error or exception occurs.
The Include Directive. This directive inserts the contents of another file in the main
JSP file, where the directive is located. You can use it for including copyright
information, scripting language files, or anything you might want to reuse in other
applications. The include directive allows you to separate your content into more
manageable elements such as including a common page header or footer. The page
included can be a static HTML page or more JSP content.
Scripting Elements
Declarations. With declarations you can define methods or variables in a JSP page
that will be accessible to other code within the same page.
Expressions. Expressions are simple JSP tags that convert the value of an
expression defined between <%= %> into a string and emits that value as dynamically
generated text. As such they are shortcuts for generating text so that you do not have
to call the println() method to display text.