BB: Javaserver Pages (JSP)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

BB: JavaServer Pages

(JSP)
ATS Application Programming: Java
Programming

© Accenture 2005 All Rights Reserved Course Code #Z16325


Objectives
This review session will cover JSP.
Included in this review:
 Overview

 What is a JSP Page?

 Syntax Summary

 Template Text: Static HTML

 JSP Scripting Elements

 JSP Directives

 JSP Actions

 Example using Scripting and Directives

 Resources ©2004 Accenture All Rights Reserved. 2


© Accenture 2005 All Rights Reserved
Overview
Choosing a server-side language used to be
easy.
Before, CGI was the only scripting option
out there.
Developers could write their own server
extensions,
but few were up to the challenge to address
the
following difficulties:
© Accenture 2005 All Rights Reserved
©2004 Accenture All Rights Reserved. 3
Overview (Cont.)
In response to ASP, Sun Microsystems gave the
world JavaServer Pages (JSP) technology, based
entirely upon Sun's popular Java programming
language, it
gives developers the advantages of developing in
Java in a
more relaxed, script-like environment.

Behind the scenes, JSP pages are dynamically


assembled ©2004 Accenture All Rights Reserved.
© Accenture 2005 All Rights Reserved
4
What is a JSP Page?
A text document that contains two types of
information:
 Static data (which can be expressed in any
text-based format (such as HTML, SVG,
WML, and XML)
 JSP elements (which construct dynamic
content)

.jsp = file extension for a JSP Page source file


©2004 Accenture All Rights Reserved. 5
© Accenture 2005 All Rights Reserved
JSP Expression
Summary of Syntax Basics
JSP Element Syntax
<%= expression %>
Interpretation
Expression is evaluated and placed in output.
JSP Scriptlet <% code %> Code is inserted in service method.
Code is inserted in body of Servlet class, outside of
JSP Declaration <%! code %>
service method.
JSP page Directive <%@ page att="val" %> Directions to the Servlet engine about general setup.
A file on the local system to be included when the JSP
JSP include Directive <%@ include file="url" %>
page is translated into a Servlet.
Comment; ignored when JSP page is translated into
JSP Comment <%-- comment --%>
Servlet.
<jsp:include
The jsp:include Action page="relative URL" Includes a file at the time the page is requested.
flush="true"/>
The Expression ${ } Expressions that retrieve the value of object properties.
Language (EL)
<jsp:useBean att=val*/> or
<jsp:useBean att=val*>
The jsp:useBean Action Find or build a Java Bean.
...
</jsp:useBean>
The jsp:setProperty Set bean properties, either explicitly or by designating
<jsp:setProperty att=val*/>
Action that value comes from a request parameter.
<jsp:getProperty
The jsp:getProperty
name="propertyName" Retrieve and output bean properties.
Action
value="val"/>
<jsp:forward
The jsp:forward Action Forwards request to another page.
page="relative URL"/>
<jsp:plugin
Generates OBJECT or EMBED tags, as appropriate to the
attribute="value"*>
The jsp:plugin Action browser type, asking that an applet be run using the
...
Java Plugin.
</jsp:plugin>©2004 Accenture All Rights Reserved. 6
© Accenture 2005 All Rights Reserved
Template Text: Static HTML

A large percent of your JSP page just


consists
of static HTML, known as Template
Text.
©2004 Accenture All Rights Reserved. 7
© Accenture 2005 All Rights Reserved
JSP Scripting
Expressions:
A JSP expression is used to insert Java values
directly into
the output.
<%= Java Expression %>

The following shows the date/time that the page


was
requested.
©2004 Accenture All Rights Reserved. 8
© Accenture 2005 All Rights Reserved
JSP Scripting
Scriptlets:
JSP Scriptlets let you insert arbitrary code into the
Servlet
Method.
<% Java Code %>

<%
String queryData = request.getQueryString();
Out.println(“Attached GET
©2004 Accenture data:
All Rights “+
Reserved. 9
© Accenture 2005 All Rights Reserved
JSP Scripting
Scriptlets

Before conversion: After conversion:


<% if (Math.random() < if (Math.random() < 0.5) {
0.5) { %> out.println(“Have a
Have a <B>nice</B> day! <B>nicew</B> day!”);
<% } else { %> } else {
Have a <B>lousy</B> day! out.println(“Have a
<% } %> <B>lousy</B> day!”);
}
©2004 Accenture All Rights Reserved. 10
© Accenture 2005 All Rights Reserved
JSP Scripting
Declarations
A JSP declaration lets you define methods or
fields that are
inserted into the main body of the Servlet class.

<%! Java Code %>

<%! Private int accessCount = 0; %>


Access to page©2004
since server
Accenture reboot:
All Rights Reserved. 11
© Accenture 2005 All Rights Reserved
JSP Directives
A JSP directive affects the overall structure of the
servlet class.

Name Description
<% directive attribute=“value” %>
Page Lets you import classes and
customize the Servlet
superclass, and the like.
Includ Lets you insert a file into the
e Servlet class at the
time the JSP file is translated into
a Servlet.
©2004 Accenture All Rights Reserved. 12
© Accenture 2005 All Rights Reserved
JSP Directives
session true (active) / false (disable)
<%@ page session = “true” %>
buffer sizekb (buffer size in Kilobyte) / none
(disable)
autoflush true <%@ (flush buffer) /=false
page buffer “none”(exception)
%>
<%@ page autoFlush = “true” %>
extends extends=“package.class”
<%@ page extends =“com.taglib…” %>
info info = “message”
<%@ page info = “java.com test page,
errorPage errorpage test“%>= “URL”
<%@ page errorPage =
isErrorPa true (error page) / false (no
“/error/error.jsp” %> error page)
ge <%@ page isErrorPage = “true”%>
language language = “java”
<%@ page language = “java” %>
contentTy contentType=”ctinfo”
pe <%@ page contentType = “description”
import import=”importList”
%>©2004 Accenture All Rights Reserved. 13
<%@ page import = “package” %>
© Accenture 2005 All Rights Reserved
JSP Directives
Include:
This directive lets you include files at the
time
the JSP page is translated into a Servlet

<HTML>
<HEAD><TITLE>JavaServer Pages
(JSP) </TITLE>
</HEAD> ©2004 Accenture All Rights Reserved. 14

<BODY>
© Accenture 2005 All Rights Reserved
JSP Actions
JSP Actions are used to control the behavior of
the
Servlet Engine.
• jsp:include - Include a file at the time the page is
requested.
• jsp:useBean - Find or instantiate a JavaBean.
• jsp:setProperty - Set the property of a JavaBean.
• jsp:getProperty - Insert the property of a JavaBean into
the output.
• jsp:forward - Forward the requester to a new page.
• jsp:plugin - Generate browser-specific code that makes
an OBJECT or EMBED tag for the Java plugin.

©2004 Accenture All Rights Reserved. 15


© Accenture 2005 All Rights Reserved
Example using Scripting &
<HTML>
Directives
<HEAD><TITLE>Using JavaServer Pages</TITLE></HEAD>

<BODY BGCOLOR="#FDF5E6" TEXT="#000000" LINK="#0000EE” VLINK="#551A8B"


ALINK="#FF0000">

<CENTER>
<TABLE BORDER=5 BGCOLOR="#EF8429">
<TR><TH CLASS="TITLE">
Using JavaServer Pages</TABLE>
</CENTER>
<P>

<UL>
<LI><B>Expression.</B><BR>
Your hostname: <%= request.getRemoteHost() %>.
<LI><B>Scriptlet. </B><BR>
<% out.println("Attached GET data: " + request.getQueryString()); %>
<LI><B>Declaration (plus expression).</B><BR>
<%! private int accessCount = 0; %>
Accesses to page since server reboot: <%= ++accessCount %>
<LI><B>Directive (plus expression). </B><BR>
<%@ page import = "java.util.*" %>
Current date: <%= new Date () %>
</UL>

</BODY>
</HTML> ©2004 Accenture All Rights Reserved. 16
© Accenture 2005 All Rights Reserved
Example using Scripting &
Output Directives

©2004 Accenture All Rights Reserved. 17


© Accenture 2005 All Rights Reserved
Resources
Helpful Websites:
 http://javaboutique.internet.com/tutorials/JSP/
 http://java.sun.com/j2ee/1.4/docs/tutorial

 http://java.sun.com/products/jsp/download.htm

 http://java.sun.com/products/jsp
 http://www.roseindia.net/jsp/introduction.shtm

 http://java.sun.com/developer/onlineTraining/J
©2004 Accenture All Rights Reserved. 18
© Accenture 2005 All Rights Reserved
Questions

©2004 Accenture All Rights Reserved. 19


© Accenture 2005 All Rights Reserved

You might also like