Sei sulla pagina 1di 4

JSP

Processing
Objectives
Understand background processing to become a better developer 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: The elements of a JSP page Server processing of JSP page 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

JSP Tags
JSP tags are of two primary types: Sriptlets tags gives you the freedom to use any kind of Java code JSP keyword tags tap into the special language features provided by the JSP libraries Scriptlet Tags Any tag starting with <% and ending with %> and any code in between, is a scriptlet tag Everything in a scriptlet tag stays on the server side, where it's compiled for processing

Format of JSP Tags You only need to understand that all JSP tags must behave properly by closing themselves with closing tag or by following the special syntax for a single unified tag. Example: <jsp:include page= flush=true> <jsp:param name=logging value=on /> </jsp:include> s

The odd looking tag in the center showcases the new shorthand, the special syntax for a single unified tag that is an abbreviation for the opening and closing tag.

The abbreviation is a forward slash inserted before the closing angle bracket ( > ) You can use this single tag syntax when two tags can't contain a value, such as with the HTML <br /> tag, or when the value is defined inside the tag.

Improved Commenting with JSP Commenting JSP codes is a necessary step toward ensuring a well-designed and easily maintained application. It is a bad habbit that regular HTML and JavaScript are rarely commented Unlike regular HTML comments, JSP comments will be read only by developers The following is an example of a regular HTML comment <!-- this text will not show up om the browser screen, but will be visible in the browser's page soure --> Compare that with an example of a JSP comment <%-- This is a JSP comment: it should NEVER show up on the client's browser, but it doesn't always work, e.g. in some versions of Nestscape and Jrun. Too bad. It would have been an excellent help, becuase then large comment block could've have easily assigned --%>

JSP Directives Comprise of two groups of tags the @ directives which provide the JavaServer Engine with compilation instructions for building a JSP The @ directives act as shortcuts allowing you to reference libraries and build a JSP page out of other HTML pages or even other JSP pages the action tags such as <jsp:forward>, which provide runtime instructions for handling of the page The action tags allow you to include HTML/JSP files at runtime or forward a user on to a URL constructed at runtime

The import Directive The @ import directives are fundamental cues that assist the JavaServer Engine in compiling a JSP page They must appear at the top of the JSP page The @ import directive can reference distinct Java classes, JavaBeans and entire libraries <%@ page import=java.util.Date %> This import tag is represented in pure Java source code as: import java.util.Date

By using these declarations, you can write code that's cleaner and easier to read. You can write: Date myDate = new Date(); instead of: java.util.Date myDate = new java.util.Date();

The include Tag o JSP can use special include tags to build a larger page for an HTTP response by including several other pages such as sandwiching its dynamic content between headers and footers For a JSP page to paste in a header, it needs only reference the include tag in the appropriate place. <jsp:include page=header.jsp flush=true

The forward Tag o Often, a web browser is redirected to a page based on some conditional processing. If a user doesnt complete a survey, they may be directed to a particular page o You can redirect the user by using the forward tag like this: <jsp:forward page=AnswerSurvey.jsp /> o With the release of JSP 1.1, developers have been allowed to attach parameters, such as in the following example: <jsp:forward page=SelectOrder.jsp> <jsp:param name=logging value=on /> </forward> o o These parameters function in the same way as values pushed to the web server using an HTTP POST jsp:param is not a valid tag unless it is sandwiched between other relevant JSP tags such as the forward tag.

The errorPage Directive o You use the errorPage directive to lay out an overall framework for handling errors. o Each JSP page should handle foreseeable errors and the JSP code should also indicate another page to deal with any unhandled exceptions. o There must be a generic error page that all catastrophic exception and errors should be forwarded and that generic page is specified by the

errorPage attribute of a JSP page object and the attribute needs to be placed near the top of the JSP page: <%@ page errorPage=MyErrorPage.jsp %> o You must place a JSP MyErrorPage.jsp file in the same directory and it must indicate its design by assigning the JSP page object member variable isErrorPage as being true <%@ isErrorPage=true

Potrebbero piacerti anche