You are on page 1of 7

JSP Processing

Objectives:
Understand background processing to become a better programmer Familiarize when to use which tool where, how to get components working together and where to look when things go wrong. To understand fully the concept of: o The elements of a JSP Page o Server Processing of JSP Page o Using JSP with other enterprise components

The Tag Elements of a JSP Page


You use JSP tags to insert Java code into a web page to ease the task of displaying or processing data. JSP tags can help structure the layout of the page by including sections from other HTML or JSP files.

The Main Tags in JSP


1. 2. 3. 4. 5. 6. Declaration Tag Expression Tag Directive Tag Scriptlet Tag Action Tag Forward Tag

Declaration Tag ( <%! %> ) This tag allows the developer to declare variables or methods Before the declaration you must have <%! At the end of the declaration, the developer must have %> Code placed in this tag must end in a semicolon ( ; ) Declarations do not generate output so are used with JSP expression or scriptlets
<%! private int counter = 0; private String getAccount(int accountNo); %>

Expression Tag ( <%= %> ) This tag allows the developer to embed any Java expression and is short for out.print() A semicolon ( ; ) does not appear at the end of the code inside the tag

Date today is <%= new java.util.Date() %>

Directive Tag ( <%@ directive %> ) A JSP directive gives special information about the page to the JSP engine. There are main three types of directives: 1. Page processing information for this page 2. Include - files to be included 3. Tag library - tag library to be used in this page Directives do not produce any visible output when the page is requested but change the way the JSP Engine process the page Page directive Below are the common page directive attributes used that provide the JSP Engine with special processing information. extends Superclass used by the JSP engine for the translated Servlet
<%@ page extends=Runnable %>

import Import all the classes in a Java package into the current JSP page. This allows the JSP page to use other Java classes.
<%@ page import=java.util.* %>

Session Boolean value that instructs the page to use sessions By default all JSP pages have session data available Default is true

isThreadSafe Can the generated Servlet deal with multiple requests? If true a new thread is started so requests are handled simultaneously

errorPage Different page to deal with errors. Must be URL to error page
<%@ page errorPage = /error/error.jsp %>

IsErrorPage

This flag is set to true to make a JSP page a special Error Page This page has access to the implicit object exception

Include Directive Allows a JSP developer to include contents of a file inside another. Typically used for navigation, tables, headers, and footers that are common to multiple pages This includes the html from privacy.html found in the include directory into the current jsp page.
<%@ include file = include/privacy.html %>

Include a navigation menu (jsp file) found in the current directory


<%@ include file = navigation.jsp %>

Tag Lib directive A tag lib is a collection of custom tags that can be used by the page Action Tag There are three main roles of action tags: o Enable the use of server side JavaBeans o Transfer control between pages o Browser independent support for applets Dynamic JSP Include The include directive is useful for including common pages that are shared and is included at compile time. To include a page at run time you should use dynamic JSP includes
<jsp:include page=URL flush=true />

JSP specification 1.1 allows for the attachment of an optional value or values using subservient tags, jsp:param as in the following:
<jsp:include page= flush=true> <jsp:param name=logging value=on /> </jsp:include>

Note that jsp:param is not a valid tag unless it is sandwiched between other relevant JSP tags, such as a forward tag. The Forward Tag Often a web user is redirected to a page based on some conditional processing. For example, if a user doesnt complete a survey, they may be redirected to a particular page. You can redirect the user by using the forward tag:
<jsp:forward page=SelectOrder.jsp> <jsp:param name=logging value=on /> </jsp:forward>

Example:

You might also like