JSP Standard Syntax • Besides HTML tag elements, JSP provides four basic categories of constructors (markup tags): directives, scripting elements, standard actions, and comments.

• You can author a JSP page either in JSP standard syntax format or in a pure XML document format. The JSP XML format document complies with the XML specification and can be used for data interchange. JSP Directives • A JSP instructs the JSP engine to configure the structure of the Servlet that is generated from the JSP pages. The JSP directives do not produce any Java target code for the translated Servlet class. • The JSP directives only affect the Servlet settings such as page settings, and inclusion of packages, classes, and custom tag libraries. JSP directives are processed at JSP translation time instead of execution time. • The general syntax of a JSP directive is:

<%@ directive attribute1=”value1” attribute2=”value2” . . . attributen=”valuen” %> JSP page Directive • The JSP page directive directs the JSP engine to specify the page setting during the Servlet translation time. It has many attributes. Some attributes have their default values. • All the attributes may only have one value except the import attribute, which may have multiple values. Here is the summary of all attributes of page directive. • The value of the attribute must be enclosed in a pair of quotation marks. The values in bold indicate the default values. The often used attributes are listed at the top of following list. JSP page Directive (cont.) JSP page Directive (cont.) JSP include Directive • The JSP include directive instructs the JSP engine to include the actual contents of another file that may be a static HTML page, text file, or JSP file. • The included file may declare methods or variables. The included file is inserted into the current page at translation time. People refer to this include directive as a static include. The syntax of include directive is:

<%@ include file=/ %>

• A banner page, heading page, copyright page, or a footnote page are perfect examples of files to be included in a JSP page. JSP taglib Directive • The taglib directive is used to include a custom tag so that the JSP can make use of the tags defined in the tag library. There are two types of tags: standard tags and user defined custom tags. • • JSP developers can encapsulate complex and often used server side tasks in custom tags for reuse purposes. This is similar to standard tags such as the JSP useBean action tag that will be introduced soon. JSP taglib Directive

• Each self-defined tag must have three components: – A tag handler class that specifies the behavior of the new defined tag – A Tag Library Descriptor (TLD) XML file with an extension .tld that describes all tags in the tag library. It maps the tag names to their tag handler classes in the library – A JSP page that uses the user defined tag. JSP taglib Directive (cont.) • The syntax of taglib directive is: • <%@ taglib uri=”” prefix=”” %> • where the Uniform Resource Identifier (URI) attribute specifies the absolute or relative URI to the TLD file. For example, you can use a custom tag in this way: • . . . •where myTag is defined in taglib.tld with the prefix p_name. JSP Scripting Elements • There are three scripting elements in JSP syntax. All of them are Java code. • Declarative elements start with <%! • Expression elements start with <%= • Scriplet elements start with <% Declaration Element • The JSP declaration element defines page-scope variables to store information or defines supporting methods that the rest of a JSP page may need. Declaration elements do not produce outputs. • The declaration content must be a valid Java statement or multiple statements separated by semicolons. The contents of the declaration scripting element will be translated into Servlet Java code and inserted into the body of the translated corresponding Servlet code somewhere outside of any methods. Declaration Element (cont.) • Its syntax is

<%! Java Code %> • For example, you can define page scope primitive or class variables as

<%! int i=0; Double d; %> Declaration Element (cont.) • You can also declare methods by declaration scripting elements. For example, you can declare the JSP life cycle methods jspInit() and jspDestroy() as follows :

<%! Connection conn %> . . . <%! public void jspInit() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = java.sql.DriverManager.getConnection( "jdbc:oracle:oci9:myDB","scott","tiger"); } catch (Exception e) { . . .} } %> Declaration Element (cont.) • This JSP method will be part of the init() method of the Servlet after JSP translation. It will be called when the object of the corresponding Servlet class is instantiated. It is called only once so that it can save a lot of resources such as database connection cost in above example.

<%! public void jspDestroy() { try { if (conn != null) conn.close(); } catch (java.sql.SQLException e) { . . .} } %> • The jspDestroy() method will be part of destroy() method of the Servlet.when the Servlet instance ends, it clears up all resources the JSP has acquired. Expression Scripting Elements • In JSP, the results of an expression evaluation are always converted to a string and directly included in the output page. Typically, expressions are used to display simple values of variables or return values of shared data by invoking a bean's getter methods. The JSP expression syntax is given here. The predefined JSP objects such as request, response, and session can also be used within the expression element to make the expression as simple as possible.

<%= expr %> // No semicolon <%= i * j + k %> <%= myBean.getSalary() %> <%= request.getRemoteHost() %> Scriptlet Element • JSP scriptlets are embedded within <% ... %> tags. They are executed at the runtime. • The syntax is <% Java code %>

• The scriptlets are inserted into the _jspService() method of the translated Servlet code which is invoked by the Servlet service() method. This java code is executed when the request is serviced by the JSP page. You can put any valid Java code within a scriptlet element, and it is not limited to one line of . For example, the following displays the counter during execution of a loop, combining the use of the expressions element and the scriptlets element:

<% for (int i=1; i<=4; i++) { %> The counter is now <% = i %> <% } %> JSP Standard Actions • JSP standard action elements simplify the access actions to other Web components such as Servlet, JSP, or JavaBean components. • The format is JSP forward Action

• This action forwards the request to another page, i.e. an internal redirect. • For example, it forwards the control from current page to second.jsp. • . . . • •. . . JSP include Action

• Addition to the JSP page include directive that includes a page at the JSP translation time, JSP also supports an action that can dynamically include a page at run-time. • ” flush=”true | false” /> • If the flush attribute is set to true, the included page will update when the JSP is updated. Remember that the include directive only copies the contents once and never changes afterwards since it is static. The purpose of include action is to include the other page to be part of current page. It is used for such as conditionally including other pages at run time.