Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Developing Web
Applications using Java
Server Pages (JSP)
JSP Basics
• 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.
• <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>
J2EE container
Web Yes
First
container 1
time
(p
page
g compiler
p )
Create servlet
http://host/date.jsp date.java
No
Compile servlet
date.class
3
Servlet life cycle
* 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
Why JSP developers need servlets?
• Classes:
– JspFactory
J F t
– PageContext
– JspEngineInfo
– JspWriter
• Exceptions:
– JspException
– JspError.
Example:
<% ! Æ(Note it is <%! JSP Declaration Not <%)
public void jspInit() {
System.out.println("Init");
}
public void jspDestroy() {
System.out.println("Destroy");
}
%>
– public
bli abstract
b t tP PageContext
C t t getPageContext
tP C t t (Servlet
(S l t
requestingServlet, ServletRequest request,
ServletResponse response, String errorPageURL,
boolean needsSession
needsSession, int buffer
buffer, boolean autoFlush)
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 18
The JspFactory Class (cont.)
• 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);
• 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
JSP Syntax
JSP tags :
Directive elements
Scripting elements
Action elements
Converting into XML syntax
JSP tags
page Scriptlets
include Declarations
taglib Expressions
• Examples
– <%@ page import="java.util.Enumeration" %>
– <%@
@ppage
g session="false" %>
– <%@ page buffer="16384" %>
– <%@ page autoFlush="false" %>
– <%@ page errorPage="ErrorPage.jsp" %>
• It’s not allowed to repeat assigned attributes
exceptt for
f import
i t attribute
tt ib t
• Note the g
generated servlet’s code.
• Syntax
<% //// put here your java code //// %>
l :
• Example
E
<%
out.println("Current
t i tl ("C t Time:
Ti " + getSystemTime())
tS t Ti ()) ;
%>
• Syntax:
<%=
% put the variable here %>
%
• Example:
<%=
% java.util.Calendar.getInstance().getTime() % %>
The same result of :
out.print( java.util.Calendar.getInstance().getTime() );
They consists of :
• jsp:include
• jsp:forward
• jsp:param
• jsp:plugin
• jsp:params
• jsp:fallback
• jsp:useBean
• jsp:setProperty
• jsp:getProperty
<jsp:forward page="relativeURL">
( <jsp:param . . . /> )*
</ jsp:forward >
• HTML comments
<!-- HTML Comment -->
<! >
• JSP comment
<%-- JSP Comment --%>
• 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
• Syntax
– <jsp:directive.directiveName attribute_list />
– <jsp:declaration>
j d l ti d l ti code
declaration d </jsp:declaration>
/j d l ti
– <jsp:scriptlet> scriptlet code </jsp:scriptlet>
– <jsp:expression>
jsp:expression expression </jsp:expression>
/jsp:expression
• 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>
What is a Bean?
<useBean> Tag Structure.
Structure
How to use a request parameter to feed the value of a
bean variable ?
• Example:
- If yyour Book bean class has a long
g instance variable
isbn and a boolean borrowed.
- Then your bean class should have the following
methods to make isbn and borrowed accessible by the
standard JSP actions:
long getIsbn ()
void setIsbn (long isbn)
boolean isBorrowed ()
void setBorrowed (boolean borrowed)
<jsp:useBean id=“bookBean“
class=" com.mybeans.MyBookBean"/>
<jsp:setProperty
jsp:setProperty name
name=" beanName "
property=“propertyName" value="value"/>
<jsp:getProperty
j pg p y name=" bookBean "
property=“isbn"/>
<jsp:useBean
jsp:useBean id
id= “myBean”
myBean
class=“com.mybeans.MyFirstBean” />
The
e First
st Name
a e is
s <jsp:getProperty
jsp get ope ty
name=“myBean” property=“firstName” />
• For Example:
<jsp:setProperty name=“bookBean” property=" isbn"
param= “isbNum”/>
• isbNum is the name of the request parameter.
• The value of the request parameter isbNum is automatically used as the
value of the bean pproperty
p y isbn.
• Type conversions from String to primitive types (byte, int, double, etc.)
and wrapper classes (Byte, Integer, Double, etc.) are automatically
performed.
p
• If the specified parameter is missing from the request, no action is taken
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 76
How to use a request parameter to feed the
value of a bean variable ? (cont’)
• page
• request
• session
i
• application
1. page:
– The default value.
– The bean is bound to a local variable (accessible by
this jsp page only)
only).
– The bean object is placed in the PageContext object
for the duration of the current request.
– The code can access it by calling getAttribute on the
predefined pageContext variable of the page.
– The
Th bean
b is
i nott shared
h d and d th
thus a new b
bean will
ill b
be
created for each request.
2. request:
– The bean is bound to a local variable
variable.
– The bean object is placed in the HttpServletRequest
object for the duration of the current request.
– The servlet code can access it by calling getAttribute
on the request variable.
– Note
N t that
th t two
t JSP pages or a JSP page and d a servlet
l t
share request objects jsp:include, jsp:forward or the
include or forward methods of RequestDispatcher
q p is
used.
3. session:
– The bean is bound to a local variable.
– The bean object will be stored in the HttpSession
object associated with the current request
request.
– The servlet code can access it by calling getAttribute
on the session variable.
4. application:
– Th
The bbean iis b
boundd tto a llocall variable.
i bl
– The bean object will be stored in the ServletContext
available through the predefined application variable
or by a call to getServletContext.
– The ServletContext is shared by all servlets and JSP
pages in
i th
the WWebb application.
li ti
– Values in the ServletContext can be retrieved with the
getAttribute method.
MVC Frameworks
• Why MVC?
1 Dividing responsibilities:
1.
• Business logic and presentation are separated.
• y change
Flexibility: g in one layer
y does not affect
the other.
2. Ease of maintenance.
1-request
Controller
(Servlet)
2
Client 3
browser
View
Model
5-response (JSP)
4
<jsp:useBean
<jsp seBean id
id="key"
"ke " ttype=“myPackage.
pe “m Package BeanObject "
scope= “application” />
JavaTM Education & Technology Services
Copyright© Information Technology Institute http://jets.iti.gov.eg 96
Notes on the Steps of Implementing MVC (cont’)
• You can place your jsp pages under the WEB-INF
directory to prevent clients from accessing them directly
and only servlets can forward requests to them.