Академический Документы
Профессиональный Документы
Культура Документы
Developing Web
Applications using Java
Server Pages (JSP)
JavaTM Education & Technology Services
Copyright Information Technology Institute
Table of Contents
http://jets.iti.gov.eg
Chapter 1
JSP Basics
Chapter 1 Outline
What is Java Server Page (JSP)?
What is wrong with servlets?
What is the difference between servlets and JSP?
Advantages
Ad
t
off JSP
JSP.
How JSP look like?
How JSP works?
javax.servlet.jsp package.
JspPage interface
HttpJspPage Interface
JSP Classes
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
http://jets.iti.gov.eg
Servlets:
Are Java programs with embedded HTML
HTML.
Generate dynamic content.
Do not separate static and dynamic content
content.
JavaServer Pages:
Are HTML pages with embedded Java code or
they can be pure XML.
Generate dynamic content
content.
Separate static and dynamic content.
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
Benefits of JSP
http://jets.iti.gov.eg
<HTML> tags stay as they are. When you need to add dynamic
content, all you need to do is enclose your code in <% %> tags
<%@ page import="java.util.Enumeration" %>
<HTML>
<HEAD><TITLE> Using JSP </TITLE></HEAD>
<BODY BGCOLOR= #DADADA>
<%
Enumeration parameters = request.getParameterNames();
String param = null;
while (parameters.hasMoreElements()) {
param = (String) parameters.nextElement();
out.println(param + ":" + request.getParameter(param) +
"<BR>");}
out.close();
();
%>
</BODY>
</HTML>
http://jets.iti.gov.eg
First
time
(p
page
g compiler
p
)
http://host/date.jsp
Yes
1
Create servlet
date.java
No
Compile servlet
date.class
http://jets.iti.gov.eg
Example
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
% out.println
t i tl ( "JSP iis easy");%>
") %
</BODY>
</HTML>
http://jets.iti.gov.eg
10
* Source: Core Servlets and Javaserver Pages By Marty Hall, Larry Brown ISBN 0130092290
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
11
http://jets.iti.gov.eg
12
javax.servlet.jsp.*
http://jets.iti.gov.eg
13
javax.servlet.jsp.* (contd)
Classes:
JspFactory
J F t
PageContext
JspEngineInfo
JspWriter
Exceptions:
JspException
JspError.
http://jets.iti.gov.eg
14
JspPage interface
The JspPage is the interface that must be implemented
byy all JSP servlet classes.
It contains two methods:
public void jspInit()
p
public void jjspDestroy()
p
y()
Example:
<% !
(Note it is <%! JSP Declaration Not <%)
public void jspInit() {
System.out.println("Init");
}
public void jspDestroy() {
System.out.println("Destroy");
}
%>
http://jets.iti.gov.eg
15
HttpJspPage Interface
http://jets.iti.gov.eg
16
JSP Classes
http://jets.iti.gov.eg
17
http://jets.iti.gov.eg
18
The following
g code is p
part of the _ jjspService
p
method that is generated by the JSP container:
JspFactory _jspxFactory
jspxFactory = null;
PageContext pageContext = null;
_jspxFactory =JspFactory.getDefaultFactory();
pageContext = _jspxFactory.getPageContext ( this,
request,
response,"",
""
true, 8192,
true);
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
19
http://jets.iti.gov.eg
20
http://jets.iti.gov.eg
21
http://jets.iti.gov.eg
22
http://jets.iti.gov.eg
23
request
javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
out
javax.servlet.jsp.JspWriter
javax servlet jsp JspWriter
session
javax.servlet.http.HttpSession
application javax.servlet.ServletContext
javax servlet ServletContext
config
javax.servlet.ServletConfig
http://jets.iti.gov.eg
24
Review
http://jets.iti.gov.eg
25
Simple
Advanced
http://jets.iti.gov.eg
26
Development.
Compilation.
Debugging.
Division of labor.
Testing.
Reuse.
http://jets.iti.gov.eg
27
Lab Exercise
28
Assignments
http://jets.iti.gov.eg
29
Chapter 2
JSP Syntax
30
Chapter 2 Outline
JSP tags :
Directive elements
Scripting elements
Action elements
Converting into XML syntax
http://jets.iti.gov.eg
31
JSP Syntax
JSP contains 2 main things :
JSP tags/
t
/ JSP elements
l
t : make
k up the
th syntax
t and
d
semantics of JSP.
Template Data : anything else ,not understood by
the JSP container
example :HTML tags
http://jets.iti.gov.eg
32
JSP tags
JSP tags
Directive
Scripting
page
Scriptlets
include
Declarations
taglib
Expressions
Action
http://jets.iti.gov.eg
33
1-Directive Elements
They are messages to the JSP container
containing information on how the JSP container
must translate a JSP page into a corresponding
servlet.
They are actually instructions to the container
not a code.
Directives Types :
Page directives
Include directives
Tag library directives (used with custom tags )
http://jets.iti.gov.eg
34
Example
<%@ page buffer="16384" session="false" %>
<%@ include file=footer.html" %>
<%@ talib uri=. prefix =c%> Advanced
http://jets.iti.gov.eg
35
Value Type
Default
import
None
contentType
"text/html;charset=ISO8859-1
session
Boolean
true
buffer
8192
autoFlush
Boolean
"true
info
String
errorPage
URL
None
isErrorPage
Boolean
"false
isThreadSafe
Boolean
"true
extends
Class name
None
g g
language
Scripting
p g language
g g name
"java"
j
http://jets.iti.gov.eg
36
Examples
http://jets.iti.gov.eg
37
http://jets.iti.gov.eg
38
Example:
<%@
@ include file=header.html" %>
Note the g
generated servlets code.
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
39
2-Scripting Elements
http://jets.iti.gov.eg
40
Example
E
l :
<%
out.println("Current
t i tl ("C
t Time:
Ti
" + getSystemTime())
tS t Ti ()) ;
%>
http://jets.iti.gov.eg
41
IIts
used
d to d
declare
l
methods
h d or variables.
i bl
Inserted into the body of the servlet class, outside
any existing
i ti methods.
th d
Can not access the implict objects of _jspService
method
Syntax:
<%! //// put your code here //// %>
Examples:
<%! String getSystemTime() {
return Calendar.getInstance().getTime().toString();
}%>
}%
<%! int x; %>
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
42
Example:
<%=
% java.util.Calendar.getInstance().getTime() %
%>
The same result of :
out.print( java.util.Calendar.getInstance().getTime() );
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
43
Assignment
Write a utility class that generates a random
number within a given range
range.
Invoke methods of this utility class from jsp page
using scripting elements
elements.
http://jets.iti.gov.eg
44
jsp:include
jsp:forward
jsp:param
jsp:plugin
jsp:params
jsp:fallback
jsp:useBean
jsp:setProperty
jsp:getProperty
http://jets.iti.gov.eg
45
http://jets.iti.gov.eg
46
http://jets.iti.gov.eg
47
http://jets.iti.gov.eg
48
Include
I l d th
the output
t t off
a page at request time
No change needed in
the main page when the
included pages change
change.
The included p
pages
g
cannot use any JSP
constructs that affect the
main page as a whole.
<%@ iinclude
l d file=
fil urll %>
http://jets.iti.gov.eg
49
Assignment
Use both jsp:include and include directive to
include header to your jsp and Note the
difference.
http://jets.iti.gov.eg
50
http://jets.iti.gov.eg
51
http://jets.iti.gov.eg
52
http://jets.iti.gov.eg
53
Comment tag
HTML comments
<!-- HTML Comment -->
<!
>
JSP comment
<%-- JSP Comment --%>
http://jets.iti.gov.eg
54
Assignment
Use <jsp: plugin> to embed Applet in your jsp
page.
g
http://jets.iti.gov.eg
55
Benefits:
The content of the JSP page can be validated against
a set of descriptions.
The JSP page can be manipulated using an XML
tool.
The JSP page can be generated from a textual
representation by applying an XML transformation
http://jets.iti.gov.eg
56
Examples:
<jsp:directive.page
j p
p g attr=value />
<jsp:scriptlet> String s; s =
request.getParameter("user"); </jsp:scriptlet>
<jsp:text>
<jsp:text>.</jsp:text>
</jsp:text>
http://jets.iti.gov.eg
57
http://jets.iti.gov.eg
58
Chapter 3
JSP and Java Beans
59
Chapter 3 Outline
What is a Bean?
<useBean> Tag Structure.
Structure
How to use a request parameter to feed the value of a
bean variable ?
http://jets.iti.gov.eg
60
What is a Bean?
A bean is a java class with certain rules defined
by the JavaBeans specification.
Beans extend no particular class and use no
particular interface.
The 3rd strategy for invoking dynamic code.
http://jets.iti.gov.eg
61
Why beans?
Why we should use beans in JSP ?
http://jets.iti.gov.eg
62
http://jets.iti.gov.eg
63
http://jets.iti.gov.eg
64
http://jets.iti.gov.eg
65
Example:
<jsp:useBean id=
id=beanName"
beanName" scope="session"
class=" com.mybeans.MyFirstBean"/>
This element can either build a new bean or
access a preexisting one in the specified scope
scope.
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
66
http://jets.iti.gov.eg
67
Syntax:
<jsp:getProperty name=" beanName "
property="propertyName"/>
This element reads and outputs the value of a
bean property.
Reading a property means calling its getXxx
method.
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
68
<jsp:setProperty
jsp:setProperty name
name=" beanName "
property=propertyName" value="value"/>
This element modifies a bean property (i.e.,
calls its setXxx method).
method)
http://jets.iti.gov.eg
69
http://jets.iti.gov.eg
70
http://jets.iti.gov.eg
71
Example
package com.mybeans;
com mybeans;
public class MyFirstBean {
private String firstName;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String name)
{
firstName name
firstName=name;
} }
Note:In beans terminology the class is said to have a String
property called firstName.
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
72
Example (cont.)
<jsp:useBean
jsp:useBean id
id= myBean
myBean
class=com.mybeans.MyFirstBean />
<jsp:setProperty name= myBean
property=firstName
p
p y
value= Ali />
The
e First
st Name
a e is
s <jsp:getProperty
jsp get ope ty
name=myBean property=firstName />
http://jets.iti.gov.eg
73
Example (cont.)
Note You can access the bean methods using
scripting
g tags
g
<jsp:useBean id= myBean
class=com.mybeans.MyFirstBean />
<%
% String
St i name = Ali ;
myBean.setFirstName (name);
%>
The First Name is <%= myBean.getFirstName()%>
Note: Whenever possible, avoid mixing the XMLcompatible jsp:useBean tags with JSP scripting elements
containing
t i i explicit
li it JJava code.
d
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
74
http://jets.iti.gov.eg
75
For Example:
<jsp:setProperty name=bookBean property=" isbn"
param= isbNum/>
http://jets.iti.gov.eg
76
http://jets.iti.gov.eg
77
page
request
session
i
application
http://jets.iti.gov.eg
78
http://jets.iti.gov.eg
79
http://jets.iti.gov.eg
80
http://jets.iti.gov.eg
81
http://jets.iti.gov.eg
82
Lab Exercise
83
Assignment
Write a jsp page that populates a Java bean with
the data from the user and forward to another
jsp page or servlet that displays the data.
http://jets.iti.gov.eg
84
Chapter 4
MVC Frameworks
85
Chapter 4 Outline
MVC Design Pattern.
Why MVC?
MVC Frameworks.
Implementing
I l
ti MVC with
ith RequestDispatcher.
R
tDi
t h
http://jets.iti.gov.eg
86
http://jets.iti.gov.eg
87
Why MVC?
Why MVC?
1 Dividing responsibilities:
1.
2. Ease of maintenance.
3 Pre and Post Processing (sec
3.
(security,
rit logging
logging, etc
etc))
http://jets.iti.gov.eg
88
MVC Frameworks
The 4th strategy for invoking dynamic code.
The original
g
request
q
is handled by
y a servlet.
The servlet invokes the business-logic and data-access
code and creates beans to represent the results , decides
which JSP page present those results and forwards the
request to it.
This servlet is the Controller.
The beans representing the results are the Model.
The jsp that presents the results are the View.
The most popular MVC
C fframework is Apache Struts.
S
JavaTM Education & Technology Services
Copyright Information Technology Institute
http://jets.iti.gov.eg
89
1-request
Controller
(Servlet)
2
3
Client
browser
5-response
View
(JSP)
Model
4
Servlet Container
Database (EIS)
http://jets.iti.gov.eg
90
http://jets.iti.gov.eg
91
http://jets.iti.gov.eg
92
http://jets.iti.gov.eg
93
http://jets.iti.gov.eg
94
http://jets.iti.gov.eg
95
http://jets.iti.gov.eg
96
http://jets.iti.gov.eg
97
Project
After the user correctly log in to the Application,
a specified home page
g is displayed
y according
g to
the priviliges of the user.
Only the managers can add, edit or delete
another employee.
Implement the MVC architecture and avoid using
direct java code in the jsp pages as much as you
can.
http://jets.iti.gov.eg
98
http://jets.iti.gov.eg
99