Anda di halaman 1dari 6

JSP Training

JSP as an abstraction of Servlets

- Back to basics. For the web application we write the HTMLs first.

- JavaServer Pages (JSP) technology lets you put segments of servlet code directly into a
static HTML page.

- When a JSP is requested by a browser, the application server creates, compiles, loads,
and runs a background servlet to execute the servlet code segments and return an HTML
page.

- JSP Extends Servlets

In a sense, JSP technology does not provide new core technologies - everything that can
be done with a JSP page, can also be done by writing a servlet. Servlets have access to
the same set of Java APIs as JSP. Pages created with JSP technology are, in fact,
compiled into servlets, so they cannot be capable of anything inherently different.

What JSP pages do, however, is enable a different, more efficient development
methodology and simplify ongoing maintenance. This is because JSP technology truly
separates the page design and static content from the logic used to generate the dynamic
content.

JSP syntax

- A JSP looks like an HTML page with servlet code segments embedded between various
forms of opening ( <% ) and closing (%>) JSP tags.

- There are no HttpServlet methods such as init, doGet, or doPost. Instead, the code that
would normally be in these methods is embedded directly in the JSP, using JSP scriptlet
tags.

- Example

The JSP technology is best described using an example. The following JSP page is very
simple; it prints the day of the month and the year, and welcomes you with either "Good
Morning" or "Good Afternoon," depending on the time of day.

The page combines ordinary HTML with a number of JSP elements:


Calls to a clock JavaBean component
An inclusion of an external file (for copyright information)
JSP expressions and scriptlets

<HTML>
<%@ page language="java" import="com.induslogic.jsp.*" %>

<H1>Welcome</H1>

<P>Today is </P>
<jsp:useBean id=="clock" class=="calendar.jspCalendar" />
<UL>
<LI>Day: <%==clock.getDayOfMonth() %>
<LI>Year: <%==clock.getYear() %>
</UL>

<% if (Calendar.getInstance().get(Calendar.AM_PM) ==== Calendar.AM) { %>


Good Morning
<% } else { %>
Good Afternoon
<% } %>
<%@ include file=="copyright.html" %>

</HTML>

The page includes the following components:

A JSP directive passes information to the JSP engine. In this case, the first line indicates
the location of some Java programming language extensions to be accessible from this
page. Directives are enclosed in <%@ and %> markers.

Fixed template data: Any tags that the JSP engine does not recognize it passes on with the
results page. Typically, these will be HTML or XML tags. This includes the Unordered
List and H1 tags in the example above.

JSP actions, or tags: These are typically implemented as standard tags or customized tags,
and have an XML tag syntax. In the example, the jsp:useBean tag instantiates the Clock
JavaBean on the server.

An expression: The JSP engine evaluates anything between <%== and %> markers. In
the List Items above, the values of the Day and Year attributes of the Clock bean are
returned as a string and inserted as output in the JSP file. In the example above, the first
list item will be the day of the year, and the second item the year.

A scriptlet is a small script that performs functions not supported by tags or ties
everything together. The native scripting language for JSP 1.0 software is based on the
Java programming language. The scriptlet in the above sample determines whether it is
AM or PM and greets the user accordingly (for daytime users, at any rate).

- JSP Directives

JSP pages use JSP directives to pass instructions to the JSP engine. These may include the
following:

JSP page directives communicate page-specific information, such as buffer and thread
information or error handling.

Language directives specify the scripting language, along with any extensions.

The include directive (shown in the example above) can be used to include an external
document in the page. A good example is a copyright file or company information, file --
it is easier to maintain this file in one central location and include it in several pages than
to update it in each JSP page. However, the included file can also be another JSP file.

A taglib directive indicates a library of custom tags that the page can invoke.

- JSP Tags

Most JSP processing will be implemented through JSP-specific XML-based tags. JSP 1.0
includes a number of standard tags, referred to as the core tags. These include:

jsp:useBean This tag declares the usage of an instance of a JavaBeans component. If the
Bean does not already exist, then the JavaBean component instantiates and registers the
tag.
jsp:setProperty This sets the value of a property in a Bean.

jsp:getProperty This tag gets the value of a Bean instance property, converts it to a string,
and puts It in the implicit object "out".

jsp:include

jsp:forward
- Predefined Variables or Implicit Objects

request, session, application, pageContext,

- EL and EL implicit objects

Prior to JSP 2.0, a page author had to use the expression <%= aName %> to
access the value of a system, as in the following example:

<someTags:aTag attribute="<%= pageContext.getAttribute("aName") %>">

or the value of a custom JavaBeans component:

<%= aCustomer.getAddress().getCountry() %>

An expression language allows a page author to access an object using a


simplified syntax. For example, to access a simple variable, you can use something like:

<someTags:aTag attribute="${aName}">

And to access a nested JavaBeans property, you would use something like:

${aCustomer.address.country}

- Custom Tags

Interfaces
Class - getter,setters, doStartTag, doEndTag, release
Deployment TLD file, web.xml
Usage in JSPs

- SimpleTag interface

- Tags That Define Variables


A simple tag can define an EL variable that can be used within the calling page.
In the following example, the iterator tag sets the value of the EL variable
departmentName as it iterates through a collection of department names.
<tlt:iterator var="departmentName" type="java.lang.String"
group="${myorg.departmentNames}">
<tr>
<td><a href="list.jsp?deptName=${departmentName}">
${departmentName}</a></td>
</tr>
</tlt:iterator>

- Communication between Tags


Custom tags communicate with each other through shared objects. There are two
types of shared objects: public and private.
In the following example, the c:set tag creates a public EL variable called
aVariable, which is then reused by anotherTag.
<c:set var="aVariable" value="aValue" />
<tt:anotherTag attr1="${aVariable}" />
Nested tags can share private objects. In the next example, an object created by
outerTag is available to innerTag. The inner tag retrieves its parent tag and then
retrieves an object from the parent. Because the object is not named, the potential
for naming conflicts is reduced.
<tt:outerTag>
<tt:innerTag />
</tt:outerTag>

- Tag Files

- Function Descriptor

JSTL Taglib

JAVASERVER Faces technology is a server-side user interface component


framework for Java technology-based web applications.
The main components of JavaServer Faces technology are as follows:
An API for representing UI components and managing their state; handling
events, server-side validation, and data conversion; defining page
navigation; supporting internationalization and accessibility; and providing
extensibility for all these features
Two JavaServer Pages (JSP) custom tag libraries for expressing UI components
within a JSP page and for wiring components to server-side
objects

The well-defined programming model and tag libraries significantly ease the
burden of building and maintaining web applications with server-side UIs. With
minimal effort, you can
Wire client-generated events to server-side application code
Bind UI components on a page to server-side data
Construct a UI with reusable and extensible components
Save and restore UI state beyond the life of server requests

JSP technology speeds the development of dynamic web pages in a number of ways:

- Separating content generation from presentation


- Emphasizing reusable compone
- Simplifying page development with tags

Anda mungkin juga menyukai