Week 5 - PowerPoint PPT Presentation

About This Presentation
Title:

Week 5

Description:

page language = 'java' contentType = 'text/html' % html body bgcolor = 'white' ... body bgcolor = 'white' This page has been visited: %= globalCounter ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 62
Provided by: mm77
Category:
Tags: bgcolor | week

less

Transcript and Presenter's Notes

Title: Week 5


1
Week 5
  • Using snoop.jsp under Jigsaw
  • Using JavaBeans with JSP
  • Instance variables and local variables
  • An application using JSP, JavaBeans, and
  • FpML
  • An application using JSP and WML
  • Next week JSP details

2
Snoop.jsp provided with the gnujsp download
produces the result.
3
(No Transcript)
4
Snoop.jsp Pg 1/11
ltHTMLgt ltHEADgt ltTITLEgtJSP snoop page is
coollt/TITLEgt lt_at_ page import"javax.servlet.http.H
ttpUtils,java.util.Enumeration"
gt lt/HEADgtltBODYgt ltH1gtJSP Snoop pagelt/H1gt ltH2gtConte
xt informationlt/H2gt ltTABLEgt ltTRgt ltTH
alignrightgtServer Infolt/THgt ltTDgtlt
application.getServerInfo() gtlt/TDgt lt/TRgt lt/TABLEgt
5
Snoop.jsp Pg. 2/11
ltH2gtRequest informationlt/H2gt ltTABLEgtltTRgt ltTH
alignrightgtRequested URLlt/THgt ltTDgtlt
HttpUtils.getRequestURL(request)
gtlt/TDgt lt/TRgtltTRgt ltTH alignrightgtRequest
methodlt/THgt ltTDgtlt request.getMethod()
gtlt/TDgt lt/TRgt ltTRgt ltTH alignrightgtRequest
URIlt/THgt ltTDgtlt request.getRequestURI()
gtlt/TDgt lt/TRgt ltTRgt ltTH alignrightgtRequest
protocollt/THgt ltTDgtlt request.getProtocol()
gtlt/TDgt lt/TRgtltTRgt ltTH alignrightgtServlet
pathlt/THgt ltTDgtlt request.getServletPath()
gtlt/TDgt lt/TRgt
6
ltTRgt ltTH alignrightgtPath infolt/THgt ltTDgtlt
request.getPathInfo() gtlt/TDgt lt/TRgt ltTRgt ltTH
alignrightgtPath translatedlt/THgt ltTDgtlt
request.getPathTranslated() gtlt/TDgt lt/TRgt ltTRgt ltTH
alignrightgtQuery stringlt/THgt ltTDgtlt
request.getQueryString() gtlt/TDgt lt/TRgt ltTRgt ltTH
alignrightgtContent lengthlt/THgt ltTDgtlt
request.getContentLength() gtlt/TDgt lt/TRgt ltTRgt ltTH
alignrightgtContent typelt/THgt ltTDgtlt
request.getContentType() gtlt/TDgt ltTRgt ltTRgt
Snoop.jsp Pg. 3/11
7
ltTH alignrightgtServer namelt/THgt ltTDgtlt
request.getServerName() gtlt/TDgt ltTRgt ltTRgt ltTH
alignrightgtServer portlt/THgt ltTDgtlt
request.getServerPort() gtlt/TDgt ltTRgt ltTRgt ltTH
alignrightgtRemote userlt/THgt ltTDgtlt
request.getRemoteUser() gtlt/TDgt ltTRgt ltTRgt ltTH
alignrightgtRemote addresslt/THgt ltTDgtlt
request.getRemoteAddr() gtlt/TDgt ltTRgt ltTRgt ltTH
alignrightgtRemote hostlt/THgt ltTDgtlt
request.getRemoteHost() gtlt/TDgt ltTRgt
Snoop.jsp Pg. 4/11
8
ltTRgt ltTH alignrightgtAuthorization
schemelt/THgt ltTDgtlt request.getAuthType()
gtlt/TDgt ltTRgt lt/TABLEgt lt Enumeration e
request.getHeaderNames() if(e ! null
e.hasMoreElements()) gt ltH2gtRequest
headerslt/H2gtltTABLEgt ltTRgt ltTH alignleftgtHeaderlt/
THgt ltTH alignleftgtValuelt/THgt lt/TRgt lt while(e
.hasMoreElements()) String k (String)
e.nextElement() gt
Snoop.jsp Pg. 5/11
9
ltTRgt ltTDgtlt k gtlt/TDgt ltTDgtlt
request.getHeader(k) gtlt/TDgt lt/TRgt lt gt lt/TAB
LEgt lt gt lt e request.getParameterNames()
if(e ! null e.hasMoreElements()) gt
Snoop.jsp Pg. 6/11
10
ltH2gtRequest parameterslt/H2gt ltTABLEgt ltTR
valigntopgt ltTH alignleftgtParameterlt/THgt ltTH
alignleftgtValuelt/THgt ltTH alignleftgtMultiple
valueslt/THgt lt/TRgt lt while(e.hasMoreElements())
String k (String) e.nextElement() Stri
ng val request.getParameter(k) String
vals request.getParameterValues(k) gt
Snoop.jsp Pg. 7/11
11
ltTR valigntopgt ltTDgtlt k gtlt/TDgt ltTDgtlt val
gtlt/TDgt ltTDgtlt for(int i 0 i lt
vals.length i) if(i gt 0) out.print("
ltBRgt") out.print(valsi) gtlt/TDgt lt/T
Rgt lt gt lt/TABLEgt lt gt
Snoop.jsp Pg. 8/11
12
Snoop.jsp Pg. 9/11
lt e getServletConfig().getInitParameterNames()
if(e ! null e.hasMoreElements())
gt ltH2gtInit parameterslt/H2gt ltTABLEgt ltTR
valigntopgt ltTH alignleftgtParameterlt/THgt ltTH
alignleftgtValuelt/THgt lt/TRgt lt while(e.hasMoreE
lements()) String k (String)
e.nextElement() String val
getServletConfig().getInitParameter(k) gt ltTR
valigntopgt ltTDgtlt k gtlt/TDgt ltTDgtlt val
gtlt/TDgt lt/TRgt
13
lt gt lt/TABLEgt lt // Attributes available
from Jserv String prefix
"org.apache.jserv." Object attrsObj
request.getAttribute("org.apache.jserv.attribute_n
ames") if ( attrsObj ! null attrsObj
instanceof Enumeration ) Enumeration
attrs (Enumeration) attrsObj gt ltPgt ltH1gtJServ
Attributeslt/H1gt available via HttpServletRequest.g
etAttribute() ltBRgt ltTABLEgt lt while (
attrs.hasMoreElements()) String
attr attrs.nextElement().toString() gt
Snoop.jsp Pg. 10/11
14
ltTRgtltTDgt lt if (
request.getAttribute(prefix attr) ! null )
out.println( prefix attr
"lt/TDgtltTDgt"
request.getAttribute(prefix attr).toString())
else
out.println( prefix attr "lt/TDgtltTDgtNULL " )
gt lt/TDgtlt/TRgt lt
gt lt/TABLEgt lt gt lt/BODYgt lt/HTMLgt
Snoop.jsp Pg. 11/11
15
Using JavaBeans with JSP
// A javabean class found in Jigsaw/Jigsaw/Www/bea
ns/MyInfoBean.class // The beans directory must
be in the classpath. // The identifier "beans"
could be any name (but in the classpath). import
java.io.Serializable public class MyInfoBean
implements Serializable private int i
public MyInfoBean() i 0 public void
seti(int x) i x public int geti()
return i
16
A JSP page that uses the bean
lt_at_ page language "java" contentType
"text/html" gt lthtmlgt ltbody bgcolor "white"
gt lt!-- Use a Java Bean --gt ltjspuseBean
id "oneInt" class "MyInfoBean"gt
lt/jspuseBeangt
Jigsaw/Www/servlet/test1.jsp
17
lt!-- Set a value in the bean --gt
ltjspsetProperty name "oneInt" property "i"
value "5" /gt lt!-- Read a value from the
bean --gt The value is ltbgt
ltjspgetProperty name "oneInt" property "i" /gt
lt/bgt lt/bodygt lt/htmlgt
18
Output of test1.jsp
19
Another example JSP/JavaBeans
lt!-- Sending two fields to a JSP from an
HTML form. --gt lthtmlgt ltheadgt
lttitlegtUser Info Entry Formlt/titlegt lt/headgt
ltbody bgcolor "white"gt ltform action
"../servlet/userinfo1.jsp" metod "post"gt

20
lttablegt lttrgt
lttdgtNamelt/tdgt lttdgtltinput type
"text" name "name" gt lt/tdgt
lt/trgt lttrgt
lttdgtSex(m,f)lt/tdgt lttdgtltinput type
"text" name "sex" gt lt/tdgt
lt/trgt lttrgt lttdgtltinput
type "submit"gt lt/tdgt lt/trgt
lt/tablegt lt/formgt lt/bodygt lt/htmlgt
21
The JSP file useinfo1.jsp
lt!-- Create a bean to hold the form results and
generate a response. --gt lt_at_ page language
"java" contentType "text/html" gt lthtmlgt
ltbody bgcolor "white" gt ltjspuseBean
id "userInfo" class "UserInfoBean"gt
lt/jspuseBeangt
22
lt!-- Modify all the properties --gt
ltjspsetProperty name "userInfo" property ""
/gt ltulgt ltligt Name ltjspgetProperty name
"userInfo" property "name" /gt ltligt Sex
ltjspgetProperty name "userInfo" property
"sex" /gt lt/ulgt lt/bodygt lt/htmlgt
23
// A simple bean to hold name and sex import
java.io.Serializable public class UserInfoBean
implements Serializable private String
name private String sex public
UserInfoBean() name null sex null
public void setName(String n) name n
public void setSex(String s) if
(s.equals("m")) sex "Male" else
if(s.equals("f")) sex "Female"
else sex null
24
public String getSex() return sex public
String getName() return name
25
(No Transcript)
26
Instance Variables and Local Variables
lt!-- BadCounter.jsp The JSP page is turned
into a servlet class when it's first
requested. One instance of the servlet
class is created by the servlet container.
A JSP declaration places the variable in the
servlet as an instance variable. The
instance data is shared by each thread.
Variables declared with a scripting element are
local to the method and hence local to the
thread. --gt
27
lt_at_ page language "java" contentType
"text/html" gt lt! int globalCounter 0
// instance data, shared by threads gt lthtmlgt
ltheadgt lttitlegtA Page WITH A COUNTERlt/titlegt
lt/headgt ltbody bgcolor "white"gt This
page has been visited lt globalCounter gt
times. ltpgt lt int
localCounter 0 gt This
counter never increases its value lt
localCounter gt lt/bodygt lt/htmlgt
28
An Application using Fpml
The goal is to allow internet users to work on
the same document. Perhaps, in the framework of
FpML, this would result in a financial
agreement. The following example works but is
only a sketch of a full solution.
29
(No Transcript)
30
ViewFpml.jsp
31
Modifyfpml.jsp
32
Values have been changed and saved
33
FixedFloatSwap.xml
lt?xml version"1.0"?gt lt!DOCTYPE
FixedFloatSwapgt ltFixedFloatSwapgt
ltNotionalgt234.0lt/Notionalgt ltFixed_Rategt5lt/Fixed_
Rategt ltNumYearsgt45lt/NumYearsgt
ltNumPaymentsgt6lt/NumPaymentsgt lt/FixedFloatSwapgt
34
A JavaBean associated with the document
// A Javabean that loads an xml tree // permits
data to be modified and // saves the new xml
tree. import java.net. import
java.io. import org.w3c.dom. import
javax.xml.parsers.DocumentBuilderFactory import
javax.xml.parsers.DocumentBuilder import
org.xml.sax. import com.sun.xml.tree.XmlDocument

35
public class DOMBean implements Serializable
XmlDocument doc double notional
double fixedRate int numYears int
numPayments public double
getNotional() return notional public
double getFixedRate() return fixedRate
public int getNumYears() return numYears
public int getNumPayments() return
numPayments public void
setChanges(String x) System.out.println(x)
saveDocument()
36
public void setNotional(double n)
notional n Node node
find("Notional", doc) node
node.getFirstChild()
node.setNodeValue("" notional)
public void setFixedRate(double n)
fixedRate n Node node
find("Fixed_Rate", doc) node
node.getFirstChild()
node.setNodeValue("" fixedRate)
37
public void setNumYears(int n)
numYears n Node node
find("NumYears", doc) node
node.getFirstChild()
node.setNodeValue("" numYears)
public void setNumPayments(int n)
numPayments n Node node
find("NumPayments", doc) node
node.getFirstChild()
node.setNodeValue("" numPayments)
38
public void saveDocument() try
PrintWriter out
new PrintWriter(
new BufferedWriter(
new FileWriter(
"c\\Jigsaw\\Jigsaw\\Jigsaw\\Www\\fpml\\FixedFloa
tSwap.xml"))) doc.write(out)
out.close()
catch(IOException e)
System.out.println(e)
39
public DOMBean() // Load the document into
the bean try
DocumentBuilderFactory
docBuilderFactory
DocumentBuilderFactory.newInstance()

DocumentBuilder docBuilder
docBuilderFactory.newDocumentBu
ilder()
InputSource is new InputSource(
"http//localhost8
001/fpml/FixedFloatSwap.xml")
doc (XmlDocument) docBuilder.parse(is)
extract(doc)

40
catch(SAXParseException err)
System.out.println("Catching raised exception")
System.out.println("Parsing
error" ",
line " err.getLineNumber()
", URI " err.getSystemId())
System.out.println(" "
err.getMessage())
catch(SAXException e)
System.out.println("Catch clause 2")
Exception x e.getException()
((x null) ? e x).printStackTrace()
catch (Throwable t)
System.out.println("Catch clause
3") t.printStackTrace()

41
public static void main(String a)
DOMBean bean new DOMBean()
bean.setNumYears(50) bean.setChanges("Save
")
42
private void extract(XmlDocument doc)
Node n doc.getDocumentElement()
NodeList list n.getChildNodes()
int j list.getLength()
int k 0 while(k lt j)
switch((list.item(k)).getNodeType())
case n.ELEMENT_NODE
if((list.item(k).getNod
eName()).equals("Notional"))
notional Double.parseDouble((list.ite
m(k).getFirstChild().getNodeValue()))

if((list.item(k).getNodeName()).equals("Fixed_Rate
")) fixedRate
Double.parseDouble((list.item(k).getFirstChild().g
etNodeValue()))
if((list.item(k).getNodeName()).equals("NumYears")
) numYears
Integer.parseInt((list.item(k).getFirstChild().get
NodeValue()))
if((list.item(k).getNodeName()).equals("NumPayment
s")) numPayments
Integer.parseInt((list.item(k).getFirstChild().g
etNodeValue()))
break
k
43
private Node find(String x, XmlDocument doc)
Node n doc.getDocumentElement(
) NodeList list
n.getChildNodes() int j
list.getLength() int k 0
while(k lt j)
switch((list.item(k)).getNodeType())
case n.ELEMENT_NODE

if((list.item(k).getNodeName()).equals(x))
return list.item(k)
k
return null
44
ViewFPML.JSP
lt!-- This JSP page allows the user to view the
existing document and make changes through
an HTML form. --gt lt_at_ page language "java"
contentType "text/html" gt lthtmlgt ltbody
bgcolor "white" gt ltjspuseBean id
"xmlData" class "DOMBean"gt lt/jspuseBeangt
45
ltform action "modifyfpml.jsp" method "post"
gt lttablegt lttrgt
lttdgtFixed_Ratelt/tdgt lttdgtltinput type
"text" name "fixedRate"gt lt/tdgt lt/trgt
lttrgt lttdgtNotionallt/tdgt
lttdgtltinput type "text" name
"notional"gt lt/tdgt lt/trgt lttrgt
lttdgtNumYearslt/tdgt lttdgtltinput type
"text" name "numYears"gt lt/tdgt lt/trgt

46
lttrgt lttdgtNumPaymentslt/tdgt
lttdgtltinput type "text" name
"numPayments"gt lt/tdgt lt/trgt lttrgt
lttdgtltinput type "Submit"gtlt/tdgt
lt/trgt lt/tablegt
47
ltulgt ltligt Notional
ltjspgetProperty name "xmlData" property
"notional" /gt ltligt Fixed Rate
ltjspgetProperty name "xmlData" property
"fixedRate" /gt ltligt Number of Years
ltjspgetProperty name "xmlData" property
"numYears" /gt ltligt Number of Payments
ltjspgetProperty name "xmlData" property

"numPayments" /gt lt/ulgt lt/bodygt lt/htmlgt
48
Modifyfpml.jsp
lt!-- This JSP file receives the data from an HTML
form submission and sends the data to a
JavaBean. --gt lt_at_ page language "java"
contentType "text/html" gt lthtmlgt ltbody
bgcolor "white" gt ltjspuseBean id
"xmlData" class "DOMBean"gt
ltjspsetProperty name "xmlData" property ""
/gt lt/jspuseBeangt
49
ltjspsetProperty name "xmlData" property
"changes" value ""/gt lt!-- Echo new data to
the user --gt ltulgt ltligt Notional
ltjspgetProperty name "xmlData" property
"notional" /gt ltligt Fixed Rate
ltjspgetProperty name "xmlData" property
"fixedRate" /gt ltligt Number of Years
ltjspgetProperty name "xmlData" property
"numYears" /gt ltligt Number of Payments
ltjspgetProperty name "xmlData" property
"numPayments" /gt lt/ulgt lt/bodygt lt/htmlgt
50
Wireless Applications
51
Physical Interactions
request
request
Mobile Base Station
Internet
response
response
request
WDP, WTP, or WSP
HTTP
52
Wireless Communication
  • Back-end Servers Still Must Process the Info
    using JSP, ASP, database, et cetera
  • Processed Info Sent to PDA or Cell Phone (Mobile
    Internet Devices)
  • Web Clipping and WML functionally similar to HTML

53
Web Clipping
  • Format Similar to HTML
  • Client Interface (designed through HTML) pre-load
    onto Palm
  • Client Interface Only Accesses Internet if
    Information not Available Locally
  • The Server Sends Requested Pages to Client in
    HTML (that palm.net automatically converts to Web
    Clipping format)

54
Web Clipping - Client
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtMy First Wireless Applt/TITLEgt
  • ltMETA name"PalmComputingPlatform"
    content"true"gt
  • lt/HEADgt
  • ltBODYgt
  • Please Enter The Following Information
  • ltFORM method"post" action"http//dewdney.heinz.c
    mu.edu8001/servlet/reflection.jsp"gt
  • ltTABLEgt
  • ltTRgtltTDgt
  • First Name
  • lt/TDgt
  • ltTDgt
  • ltINPUT type"text" name"f_name"gt
  • lt/TDgt
  • lt/TRgt

Continued . . .
55
Web Clipping - Client
  • ltTRgt
  • ltTDgt
  • Last Name
  • lt/TDgt
  • ltTDgt
  • ltINPUT type"text" name"l_name"gt
  • lt/TDgt
  • lt/TRgt
  • ltTRgt
  • ltTD colspan2 align"right"gt
  • ltINPUT type"submit"gt
  • lt/TDgt
  • lt/TRgt
  • lt/TABLEgt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

56
Web Clipping - Server
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtMy First Wireless JSPlt/TITLEgt
  • ltMETA name"PalmComputingPlatform"
    content"true"gt
  • lt/HEADgt
  • ltBODYgt
  • You Entered the Following Information ltPgt
  • First Name lt request.getParameter("f_name") gt
    ltBRgt
  • Last Name lt request.getParameter("l_name") gt
    ltPgt
  • Thanks for using our service.
  • lt/BODYgt
  • lt/HTMLgt

reflection.jsp
57
Web Clipping Result
58
WML
  • Not Directly Supported By Palm, but 3rd Party
    Browsers Available
  • Used In Cell Phones, Pagers, PDAs, etc.
  • Implementations Vary Widely across Manufacturers
    and Models

59
WML Load Page
  • lt_at_ page contentType"text/vnd.wap.wmlcharsetISO
    -8859-1" gt
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML
    1.1//EN"
  • "http//www.wapforum.org/DTD/wml_1.1.xml"gt
  • ltwmlgt
  • ltcard id"main" title"The Input Page"gt
  • ltpgt
  • Please Enter some Info for us ltbr/gt
  • First ltinput name"f_name" /gt ltbr/gt
  • Last ltinput name"l_name" /gt
  • ltanchorgt
  • Submit
  • ltgo method"post" href"http//dewdney.heinz.c
    mu.edu/servlet/reflection2.jsp"gt
  • ltpostfield name"f_name" value"(f_name)" /gt
  • ltpostfield name"l_name" value"(l_name)" /gt
  • lt/gogt
  • lt/anchorgt
  • lt/pgt

60
WML Server Side
  • lt_at_ page contentType"text/vnd.wap.wmlcharsetISO
    -8859-1" gt
  • lt?xml version"1.0"?gt
  • lt!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML
    1.1//EN"
  • "http//www.wapforum.org/DTD/wml_1.1.xml"gt
  • ltwmlgt
  • ltcard id"main" title"My First Wireless WML
    Response"gt
  • ltpgt
  • Here is the information you entered.
  • lt/pgt
  • ltpgt
  • First lt request.getParameter("f_name") gt
    ltbr/gt
  • Last lt request.getParameter("l_name") gt
  • lt/pgt
  • lt/cardgt
  • lt/wmlgt

reflection2.jsp
61
WML - Result
Write a Comment
User Comments (0)
About PowerShow.com