Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

4 Snoop.jsp Pg 1/11 JSP snoop page is cool JSP Snoop page Context information Server Info:

5 Request information Requested URL: Request method: Request URI: Request protocol: Servlet path: Snoop.jsp Pg. 2/11

6 Path info: Path translated: Query string: Content length: Content type: Snoop.jsp Pg. 3/11

7 Server name: Server port: Remote user: Remote address: Remote host: Snoop.jsp Pg. 4/11

8 Authorization scheme: <% Enumeration e = request.getHeaderNames(); if(e != null && e.hasMoreElements()) { %> Request headers Header: Value: <% while(e.hasMoreElements()) { String k = (String) e.nextElement(); %> Snoop.jsp Pg. 5/11

9 <% } %> <% } %> <% e = request.getParameterNames(); if(e != null && e.hasMoreElements()) { %> Snoop.jsp Pg. 6/11

10 Request parameters Parameter: Value: Multiple values: <% while(e.hasMoreElements()) { String k = (String) e.nextElement(); String val = request.getParameter(k); String vals[] = request.getParameterValues(k); %> Snoop.jsp Pg. 7/11

11 <% for(int i = 0; i < vals.length; i++) { if(i > 0) out.print(" "); out.print(vals[i]); } %> <% } %> <% } %> Snoop.jsp Pg. 8/11

12 <% e = getServletConfig().getInitParameterNames(); if(e != null && e.hasMoreElements()) { %> Init parameters Parameter: Value: <% while(e.hasMoreElements()) { String k = (String) e.nextElement(); String val = getServletConfig().getInitParameter(k); %> Snoop.jsp Pg. 9/11

13 <% } %> <% // Attributes available from Jserv String prefix = "org.apache.jserv."; Object attrsObj = request.getAttribute("org.apache.jserv.attribute_names"); if ( attrsObj != null && attrsObj instanceof Enumeration ) { Enumeration attrs = (Enumeration) attrsObj; %> JServ Attributes available via HttpServletRequest.getAttribute() <% while ( attrs.hasMoreElements()) { String attr = attrs.nextElement().toString(); %> Snoop.jsp Pg. 10/11

14 <% if ( request.getAttribute(prefix + attr) != null ) { out.println( prefix + attr + " " + request.getAttribute(prefix + attr).toString()); } else { out.println( prefix + attr + " NULL " ); } %> <% } %> <% } %> Snoop.jsp Pg. 11/11

15 Using JavaBeans with JSP // A javabean class found in Jigsaw/Jigsaw/Www/beans/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 <jsp:useBean id = "oneInt" class = "MyInfoBean"> Jigsaw/Www/servlet/test1.jsp

17 The value is

18 Output of test1.jsp

19 Another example JSP/JavaBeans <!-- Sending two fields to a JSP from an HTML form. --> User Info Entry Form

20 Name: Sex(m,f):

21 The JSP file –useinfo1.jsp <!-- Create a bean to hold the form results and generate a response. --> <jsp:useBean id = "userInfo" class = "UserInfoBean">

22 Name: Sex:

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

26 Instance Variables and Local Variables <!-- 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. -->

27 <%! int globalCounter = 0; // instance data, shared by threads %> A Page WITH A COUNTER This page has been visited: times. <% int localCounter = 0; %> This counter never increases its value:

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

30 ViewFpml.jsp

31 Modifyfpml.jsp

32 Values have been changed and saved

33 FixedFloatSwap.xml 234.0 5 45 6

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\\FixedFloatSwap.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.newDocumentBuilder(); InputSource is = new InputSource( "http://localhost:8001/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 < j) { switch((list.item(k)).getNodeType()) { case n.ELEMENT_NODE : if((list.item(k).getNodeName()).equals("Notional")) notional = Double.parseDouble((list.item(k).getFirstChild().getNodeValue())); if((list.item(k).getNodeName()).equals("Fixed_Rate")) fixedRate = Double.parseDouble((list.item(k).getFirstChild().getNodeValue())); if((list.item(k).getNodeName()).equals("NumYears")) numYears = Integer.parseInt((list.item(k).getFirstChild().getNodeValue())); if((list.item(k).getNodeName()).equals("NumPayments")) numPayments = Integer.parseInt((list.item(k).getFirstChild().getNodeValue())); 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 < 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 <!-- This JSP page allows the user to view the existing document and make changes through an HTML form. --> <jsp:useBean id = "xmlData" class = "DOMBean">

45 Fixed_Rate: Notional: NumYears:

46 NumPayments:

47 Notional: Fixed Rate Number of Years Number of Payments: <jsp:getProperty name = "xmlData" property = "numPayments" />

48 Modifyfpml.jsp <!-- This JSP file receives the data from an HTML form submission and sends the data to a JavaBean. --> <jsp:useBean id = "xmlData" class = "DOMBean">

49 Notional: Fixed Rate Number of Years Number of Payments:

50 Wireless Applications

51 Physical Interactions Mobile Base Station Internet request response 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 My First Wireless App Please Enter The Following Information: First Name: Continued...

55 Web Clipping - Client Last Name:

56 Web Clipping - Server My First Wireless JSP You Entered the Following Information First Name: Last Name: Thanks for using our service. reflection.jsp

57 Web Clipping – Result

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

59 WML – Load Page <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> Please Enter some Info for us First: Last: Submit

60 WML – Server Side <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> Here is the information you entered. First: Last: reflection2.jsp

61 WML - Result


Download ppt "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."

Similar presentations


Ads by Google