Professional Documents
Culture Documents
JSP
12/13/2014
6-1
Concept:
Server-side programming
CGI scripts
servlet
Java or JavaScript
6-2
JSP is servlet
examine request:
produce response
12/13/2014
6-3
JSP page
html
JSP scripting elements
JSP directives
JSP actions
JSP comment
<%--
12/13/2014
--%>
6-4
declaration
<%!
%>
expression
<%=
%>
scriptlet
<%
12/13/2014
%>
6-5
JSP declaration
12/13/2014
6-6
JSP expression
12/13/2014
6-7
JSP scriptlet
12/13/2014
6-8
12/13/2014
6-9
12/13/2014
6-10
JSP directives
12/13/2014
6-11
has attributes
import
content-type
isThreadSafe
12/13/2014
6-12
session
buffer="sizekb|none
autoflush="true|false
extends="package.class
info="message
errorPage="url
isErrorPage="true|false
language="java"
12/13/2014
6-13
Predefined variables
request
response
instance of HttpServletRequest
ex: request.getParameter(username);
instance of HttpServletResponse
ex: response.addCookie(myCookie);
12/13/2014
6-14
Predefined variables
out
instance of JSPWriter
buffered version of output stream
ex: out.println(hello world);
12/13/2014
6-15
Predefined variables
session
instance of HttpSession
only available if session attribute of
12/13/2014
6-16
Predefined variables
application
instance of HttpContext
ex: application.setAttribute(user, John Doe);
ex: name = (String)application.getAttribute(user);
config
instance of HttpConfig
page
12/13/2014
6-17
6-18
12/13/2014
6-19
JSP actions
jsp:include
jsp:forward
jsp:useBean
jsp:setProperty
jsp:getProperty
jsp:plugin
12/13/2014
6-20
jsp:include action
<jsp:include page="relative URL" flush="true" />
6-21
jsp:forward action
<jsp:forward page="relative URL" />
12/13/2014
6-22
12/13/2014
6-24
12/13/2014
6-25
Greeting.html
<html><head>
<title>Shopping cart example</title></head>
<body><h1>Welcome to the Mini Store</h1>
<form action="MiniStore.jsp" method="post">
Please select one or more of our products:<br>
<select name="products" multiple>
<option>product 1 <option>product 2
<option>product 3 <option>product 4
<option>product 5 <option>product 6 </select>
<input type="submit">
</form>
</body></html>
12/13/2014
6-26
MiniStore.jsp (1/3)
<%@ page import="java.util.*" %>
<HTML><BODY>
<%
if (session.isNew()) {
%>
<h1>Welcome Newcomer</h1>
<% } else { %>
<h1>Welcome Back</h1>
<% } %>
12/13/2014
6-27
MiniStore.jsp (2/3)
<%
Vector list = (Vector)
session.getAttribute("cart");
if (list == null)
list = new Vector();
String products[] =
request.getParameterValues("products");
if (products != null)
for (int i = 0; i < products.length; i++)
list.addElement(products[i]);
session.setAttribute("cart", list);
%>
12/13/2014
6-28
MiniStore.jsp (3/3)
<H2>Thanks for your selection:</H2>
<TABLE><TR><TH>Product</TH></TR>
<%
Enumeration all = list.elements();
while (all.hasMoreElements())
out.println("<TR><TD>" + all.nextElement()
+"</TD>");
%>
</tr></table>
<jsp:include page="continue.html"/>
</body></html>
12/13/2014
6-29
continue.html
<form action="Greeting.html">
<input type="submit" value="Continue
Shopping">
</form>
12/13/2014
6-30
jsp:useBean action
<jsp:useBean id="name" class="package.class" />
12/13/2014
6-31
jsp:useBean action
scope attribute
<jsp:useBean id="name"
class="package.class"
scope=value/>
values can be
page
(default)
request
session
application
12/13/2014
6-32
jsp:setProperty action
<jsp:setProperty name="orderBean"
property="numberOfItems"
value=21" />
<jsp:setProperty name="orderBean"
property="numberOfItems"
param="numItems" />
sets value of bean property
12/13/2014
6-33
jsp:getProperty action
<jsp:getProperty name="itemBean"
property="numItems" />
12/13/2014
6-34
Bean example
package jspexamples;
public class Counter {
private int value = 0;
public int getValue() {
return value;
}
public void setValue(int newValue) {
value = newValue;
}
}
12/13/2014
6-35
JSP example
<html>
<head>
<title>Bean JSP example</title>
</head>
<body>
<jsp:useBean id="counter" class="jspexamples.Counter"/>
<jsp:setProperty name="counter"
property="value" value="10"/>
Current value is
<jsp:getProperty name="counter" property="value"/>
</body></html>
12/13/2014
6-36
Variation in scope
request
example
<jsp:useBean id=counter"
class=jspexamples.Counter"
scope=request/>
12/13/2014
6-37
Variation in scope
other scopes
session
application
12/13/2014
6-38
12/13/2014
6-39
12/13/2014
6-40
Advanced Exercise
12/13/2014
6-41
ShoppingCart bean
package mypackage;
import java.util.*;
public class ShoppingCart {
private Vector<String> products = new Vector<String>();
public String getTable() {
StringBuffer buf = new StringBuffer();
Enumeration all = products.elements();
while (all.hasMoreElements())
buf.append("<TR><TD>" + all.nextElement() + "</TD>");
return buf.toString();
}
public void setAddOns(String[] addOns) {
for (int i = 0; i < addOns.length; i++)
products.addElement(addOns[i]);
}
}
12/13/2014
6-42
MiniBeans.jsp
<HTML><BODY>
<H2>Thank you for your selection</H2>
<jsp:useBean id="cart"
class=mypackage.ShoppingCart" scope="session"/>
<jsp:setProperty name="cart" property="addOns"
param="products"/>
<TABLE><TR><TH>Content of Shopping Cart:</TH></TR>
<jsp:getProperty name="cart" property="Table"/>
</TR></TABLE>
<jsp:include page="continue.html" />
</BODY></HTML>
12/13/2014
6-43