Beruflich Dokumente
Kultur Dokumente
JSP Page
A JSP page is a page created by the web developer that includes JSP
technology-specific tags, declarations, and possibly scriptlets, in
combination with other static HTML or XML tags.
A JSP page has the extension .jsp; this signals to the web server that
the JSP engine will process elements on this page.
Pages built using JSP technology are typically implemented using a
translation phase that is performed once, the first time the page is
called. The page is compiled into a Java Servlet class and remains in
server memory, so subsequent calls to the page have very fast
response times.
Put .jsp pages into a WAR file (see Web ARchive)
Deploy (upload) the WAR file as a Web Application to your Sun
Java System Application Server Platform.
Overview
JavaServer Pages (JSP) lets you separate the dynamic
part of your pages from the static HTML.
HTML tags and text
<% some JSP code here %>
HTML tags and text
<I>
<%= request.getParameter("title") %>
</I>
You normally give your file a .jsp extension, and typically
install it in any place you could place a normal Web page
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
Translation Time
A JSP application is usually a collection of JSP files,
HTML files, graphics and other resources.
A JSP page is compiled when your user loads it into a
Web browser
1.
When the user loads the page for the first time, the files that make up
the application are all translated together, without any dynamic data,
into one Java source file (a .java file)
2.
Server
Server
Create
CreateSource
Sourcefrom
fromJSP
JSP
.java
.javaCompile
Compileservlet
servlet
File
File
Changed
Changed
Execute
ExecuteServlet
Servlet
JSP Elements
Declarations
Expressions
Scriplets
HTML Comment
Generates a comment that is sent to the client.
Syntax
<!--
Example:
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %>
-->
10
Declaration
Declares a variable or method valid in the scripting language used in
the JSP page.
Syntax
... %>
Examples
<%! String destin; %>
<%! Public String getDestination()
{return destin;}%>
<%! Circle a = new Circle(2.0); %>
You can declare any number of variables or methods within one declaration
element, as long as you end each declaration with a semicolon.
The declaration must be valid in the Java programming language.
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
11
Declaration Example
<HTML>
<HEAD><TITLE>JSP Declarations</TITLE></HEAD>
<BODY><H1>JSP Declarations</H1>
<%! private int keepCount = 0; %>
<H2>
Page accessed:
<%= ++keepCount %>
times
</H2>
</BODY>
</HTML>
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
12
cookies
response
out -
Object of HttpServletResponse
session -
application config -
Object of ServletConfig
pageContext page
13
Expression
Contains an expression valid in the scripting language used in the JSP
page.
Syntax
14
Expression Example
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time:
<%= new java.util.Date() %>
<LI>Your hostname:
<%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
</UL>
</BODY>
</HTML>
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
15
Scriptlet
Contains a code fragment valid in the page scripting
language.
Syntax
16
Scriplet Example
<HTML>
<HEAD><TITLE>Weather</TITLE></HEAD>
<BODY>
<H2>Today's weather</H2>
<% if (Math.random() < 0.5) { %>
Today will be a <B>suny</B> day!
<% } else { %>
Today will be a <B>windy</B> day!
<% } %>
</BODY>
</HTML>
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
17
JSP Lifecycle
Servlet from
JSP
Init Event
Request
Response
Destroy Event
jspInit()
jspInit()
jspService()
jspService()
jspDestroy()
jspDestroy()
18
Syntax:
<%@ directive attribute=value %>
<%@ directive attribute1=value1
attribute1 =value2 %>
1. page
2. include
3. taglib
XML form:
<jsp:directive.directiveType attribute=value />
19
Page Directive
Defines attributes that apply to an entire JSP page.
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet
]"
[ isErrorPage="true|false" ] - try diff
example google it
%>
20
Include Directive
Includes a static file in a JSP file, parsing the file's JSP elements.
Syntax
21
Taglib Directive
Defines a tag library and prefix for the custom
tags used in the JSP page.
Syntax
<%@ taglib uri="URIToTagLibrary"
%>
prefix="tagPrefix"
22
Server Redirection
One can forward to a text file (HTML), a CGI script, a servlet or another
JSP page.
One can only forward to a new page, provided no output of the original
page has been sent to the browser.
One may pass as many parameters as one needs with this method by
using the param tag.
The forward action ends execution of the current JSP page and removes
any existing buffered output. The new page has access to application,
request, and session objects as the starting file. A new pageContext
object is generated for the page. To the browser, it will appear you have
the originally requested page, not the page to which you are transferred .
Example:
<jsp:forward page="home/Default.jsp" >
<jsp:param name="source" value="entry"/>
</jsp:forward>
23
<jsp:forward>
Forwards a client request to an HTML
file, JSP file, or servlet for processing.
Syntax
<jsp:forward page="{relativeURL | <%= expression
%>}" />
<jsp:forward page="{relativeURL | <%= expression
%>}" >
<jsp:param name="parameterName"
value="{parameterValue | <%= expression
%>}" />+
</jsp:forward> try one example
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
24
<jsp:useBean>
Locates or instantiates a bean with a specific
name and scope.
<jsp:useBean
id="beanInstanceName"
scope="page|request|session|application"
{
class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{package.class | <%= expression %>}
}
{
/> |
> other elements
</jsp:useBean>
}
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
25
scope="page|request|session|application
<jsp:useBean id="user" scope="session"
class="allpackage.UserMaster" />
page One can use the bean within the JSP page with the <jsp:useBean> element or any of
the page's static include files, until the page sends a response back to the client or forwards a
request to another resource
request
One can use the bean from any JSP page processing the same request, until a JSP
page sends a response to the client or forwards the request to another resource. One can use the
request object to access the bean, for example,
request.getAttribute(beanInstanceName).
session
One can use the bean from any JSP page in the same session as the JSP page that
created the bean. The bean exists across the entire session, and any page that participates in the
session can use it. The page in which you create the bean must have a page directive with
session="true".
application
One can use the bean from any JSP page in the same application as the JSP
page that created the bean. The bean exists across an entire JSP application, and any page in
the application can use the
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
26
Different Scope
Most visible
Application
Application
Session
Session
Request
Request
Page
Page
Least visible
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
27
<jsp:setProperty>
Sets a property value or values in a bean.
Syntax
<jsp:setProperty
name="beanInstanceName"
{ property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{string | <%= expression %>}"
}
/>
Examples
<jsp:setProperty name="mybean" property="*" />
<jsp:setProperty name="mybean" property="username" />
<jsp:setProperty name="mybean" property="username" value="Steve" />
The <jsp:setProperty> element sets the value of one or more properties in a bean,
using the bean's setter methods. You must declare the bean with<jsp:useBean>
before
you set a property value with <jsp:setProperty>.
28
<jsp:getProperty>
Gets the value of a bean property so that you can
display it in a result page.
Syntax
<jsp:getProperty name="beanInstanceName
property="propertyName" />
Example:
<jsp:useBean id="calendar" scope="page" class="employee.Calendar" />
<h2>
Calendar of <jsp:getProperty name="calendar" property="username" />
</h2>
29
30
31
32
JSP Form
<jsp:useBean id='nb' scope='session' class=packBeans.NameBean'/>
<jsp:setProperty name='nb' property="*"/>
<HTML>
<BODY>
<H1>Please enter your name to be registered to JSP Course?</H1>
<FORM method="get">
<INPUT type="text" name="name" size="25">
<INPUT type="submit" value="Submit">
</FORM>
<%
if ( request.getParameter("name") != null ) } %>
<%=
"Click<a href=GetName.jsp> here</a> to confirm your registration"
%>
<% } %>
</BODY>
</HTML>
33
JSP Results
<jsp:useBean id='nb'
scope="sessionclass=packBeans.NameBean"/>
<HTML>
<HEAD>
<TITLE>Registered Name</TITLE>
</HEAD>
<BODY>
<jsp:getProperty name="nb" property="name"/>
</BODY>
</HTML>
34
35
Conclusion
JavaServer Pages (JSP) lets you separate the dynamic
part of your pages from the static HTML.
1. One can simply write the regular HTML in the normal
manner, using whatever Web-page-building tools you
normally use.
2. One can enclose then the code for the dynamic parts in
special tags, most of which
start with
"<%"
and end with "%>"
Copyright @ 2000 Jordan Anastasiade. All rights reserved.
36