1 / 26

Java Web应用开发:J2EE和Tomcat

蔡 剑 , Ph.D. Java Web应用开发:J2EE和Tomcat. 本讲内容. Web 层技术 ( III) Custom Tags JSP and XML JSTL. Request. JSP. JavaBeans. JSP. JavaBeans. Response. Database. Web Container. Browser. Server. Review: Use JavaBeans with JSP. JSP handles the face. JavaBean handles the logic and complexity.

susan
Download Presentation

Java Web应用开发:J2EE和Tomcat

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 蔡 剑, Ph.D. Java Web应用开发:J2EE和Tomcat

  2. 本讲内容 • Web层技术 (III) • Custom Tags • JSP and XML • JSTL

  3. Request JSP JavaBeans JSP JavaBeans Response Database Web Container Browser Server Review: Use JavaBeans with JSP JSP handles the face. JavaBean handles the logic and complexity.

  4. Custom JSP Tag • Simple Custom Tag: • <ics:tagname/> • Attribute Custom Tag: • <ics:checkinputdatabasename="<%=userBean.getDatabase()%>"/> • Use tld file to config the tag attributes • 3. Body Custom Tag: • <ics:display format="smalltable"> • <% user.checkstatus(); %> • <table ……><strong> • …… • <table> • <ics:log message ="Table printed"/> • </ics:display>

  5. Custom JSP Tag (Con’t) 4. Custom Tag can be used in Script: <ics:connection id="icdb" type="DataSource" name="icworkdatabase" /> <% icdb.getConnection(); %>

  6. Custom Tag Development • 采用特定的API编写实现标签处理的程序 • 位置 /WEB-INF/classes or /WEB-INF/lib • 建立一个tag library descriptor(TLD)文件用来描述custom tag • 位置 /WEB-INF

  7. Tag Library Descriptor <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>ics</short-name> <uri>http://www.icconcept.com/taglibs/sampleTag</uri> <display-name>IcsampleTag</display-name> <description>Sample TLD in JWAD Book</description>

  8. Tag Library Descriptor (Con’t) <tag> <name>wordcount</name> <tag-class>jwadbook.taglib.WordCount</tag-class> <body-content>empty</body-content> <attribute> <name>inputname</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>warnEmpty</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag> …… </taglib>

  9. A Simple Tag public class TimeTag implements Tag { … public int doStartTag() throws JspException { try { calendar = Calendar.getInstance(); pageContext.getOut().print( DateFormat.getDateInstance().format(calendar.getTime())+" "+ DateFormat.getTimeInstance().format(calendar.getTime()) ); } catch (IOException tage) { throw new JspException("TimeTag Error: "+ tage.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException { return SKIP_PAGE; }

  10. Simple Tag Result <HR/> <CENTER><H3> Current Time is: <ics:time/> </H3></CENTER>

  11. A Attribute Tag public class WordCount extends TagSupport { public void setInputname(String _inputname) { this.inputname = _inputname; } public void setWarnEmpty(boolean _warnempty) { this.warnEmpty = _warnempty; } public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); inputword = pageContext.getRequest().getParameter(inputname); try{if ((inputword==null)&&(warnEmpty==true)) { ……

  12. A Attribute Tag else { StringTokenizer st = new StringTokenizer(inputword); int wordnumber = st.countTokens(); out.println(wordnumber); } } catch (IOException ioe) { System.out.println ("Error: " + ioe.getMessage()); } return EVAL_PAGE; } }

  13. Tld for the Example <tag> <name>wordcount</name> <tag-class>jwadbook.taglib.WordCount</tag-class> <body-content>empty</body-content> <attribute> <name>inputname</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>warnEmpty</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag>

  14. JSP with Custom Tag <TEXTAREA rows="10" cols="58" name="input">Enter A Brief Description Here</TEXTAREA> <BR></BR> Your have entered <ics:wordcount inputname="input" warnEmpty="true" /> words in the above text area. <BR></BR> <INPUT type="submit" name="Submit" value="Count Words"></FORM>

  15. BodyTagSupport Sequences

  16. BodyTagSupport Example public class ListTag extends BodyTagSupport { private int times; public void setTimes(int _times) { if(_times>0) { times = _times; } else { times = 1; } } public int doStartTag() { if (times >= 1) { String timeString = String.valueOf(times); pageContext.getSession().setAttribute("ListTagTime", timeString); return(EVAL_BODY_INCLUDE); } else { return(SKIP_BODY); } } <TABLE> <ics:list times="4"> <ics:row/> </ics:list> </TABLE>

  17. BodyTagSupport Example public void doInitBody() { } public int doAfterBody() { if (times-- > 1) { String timeString = String.valueOf(times); pageContext.getSession().setAttribute("ListTagTime", timeString); return(EVAL_BODY_AGAIN); } else { return(SKIP_BODY); } } }

  18. RowTag inside ListTag public class RowTag extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); int rowtime = 0; rowtime = Integer.parseInt((String)pageContext.getSession().getAttribute("ListTagTime")); out.print("<TR><TD>Row Number: "+String.valueOf(rowtime)+" Created in RowTag</TD></TR>"); } catch(IOException ioe) { System.err.println("RowTag error"); } return(EVAL_BODY_INCLUDE); } }

  19. BodyTagSupport Example Result

  20. Query XMLDB Client HTML XSL XML Client XML XML XSL XML XML Servlet/Jsp Server Servlet/Jsp Server Client WML DOM/SAX EJB EJB JDBC EIS DB RDMS JDBC XML and Web Components

  21. XML Events <tasklist> <task> <taskid> JOB101 </taskid> <name> Prepare the design Requirements </name> <start> 10/1/2002 </start> <end> 10/10/2002 </end> </task> …… </tasklist> Parser startElement(“tasklist”, …) startElement(“task”, …) startElement(“taskid”, …) characters(char[],start,length) endElement(“taskid”, …) startElement(“name”, …) …… endElement(“tasklist”, …) XML Parsing using SAX

  22. XML DOM Tree <tasklist> <task> <taskid> JOB101 </taskid> <name> Prepare the design Requirements </name> <start> 10/1/2002 </start> <end> 10/10/2002 </end> </task> …… </tasklist> Parser XML Parsing using DOM

  23. A Example of Using DOM public static void main(String argv[]) { if (argv.length != 1) { System.err.println("Usage: java ProcessParser filename"); System.exit(1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse( new File(argv[0]) ); makeFrame(); } catch (SAXException sxe) { // Error generated during parsing) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } // main

  24. Web Server Custom Tag XML JSP XML SAX/DOM XML JavaBeans JSP using XML

  25. Client HTML Web Server Custom Tag Client XML XML XSLT JSP XML XML JavaBeans Client WML JSP using XSLT to Convert XML

More Related