120 likes | 277 Views
CSIT600b: XML Programming XML Programming Guide: Getting Started 2. Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial. Assignment Overview. Browser access. WAP access. XSLT experiment - interactive try and error with IE6. HTML. WML. XSLT2. XSLT3. Jtidy Parser. XSLT1.
E N D
CSIT600b: XML Programming XML Programming Guide: Getting Started 2 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial
Assignment Overview Browser access WAP access • XSLT experiment - interactive try and error with IE6 HTML WML XSLT2 XSLT3 Jtidy Parser XSLT1 Existing HTML sites Info Summary HTML J2EE environment Dickson Chiu 2004
Parsing a Web page with Jtidy • Better parser than the standard Java library • Clean up malformed and faulty HTML • More functions • Installation • Download from http://jtidy.sourceforge.net/ • Put Tidy.jar into H:\Sun\AppServer\lib • Edit H:\Sun\AppServer\j2eetutorial14\examples\common\targets.xml • Add red line for library <path id="classpath"> <fileset dir="${j2ee.home}/lib"> <include name="j2ee.jar"/> <include name="Tidy.jar"/> </fileset> </path> Dickson Chiu 2004
Parsing source code // thanks to the TA, Mr Kawah Wong import javax.servlet.http.*; import javax.servlet.*; import java.io.*; import java.net.*; import org.w3c.tidy.*; import org.w3c.dom.*; public class Html2Dom extends HttpServlet { public Html2Dom() {} protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String urlStr = req.getParameter("url"); // get the parameter URL url = new URL(urlStr); // open the connection with that url URLConnection cn = url.openConnection(); // parse the html file into dom Tidy tidy = new Tidy(); tidy.setIndentContent(true); tidy.setXHTML(true); tidy.setWraplen(Integer.MAX_VALUE); Document doc = tidy.parseDOM(cn.getInputStream(), null); // print out the Dom tidy.pprint(doc, resp.getOutputStream()); } } Dickson Chiu 2004
Front Page Interface <html> <head> <title> CSIT600B HTML to DOM Demo </title> </head> <body> <h2>HTML to DOM Demo</h2> <form method="get" action="./result"> <label>URL: </label> <input type="text" name="url" style="width:800"/> <input type="submit" name="submit" value="convert"/> </form> </body> </html> Dickson Chiu 2004
WAR Structure • Duplicate directory of example hello2 for testing • Include Tidy.jar • Alias /result for the servlet • Context root /html2dom • You don’t need the frontpage, e.g.: http://localhost:7999/html2dom/result?url=http://www.ust.hk • View the source and find it pretty-print with indentation Dickson Chiu 2004
Bonus – JSP Brief Overview • J2EE tutorial Chap 12, 13 • static data + JSP elements, which construct dynamic content • Custom tag in tag library for reuse • Use JavaBeans components for interfacing • Properties - Read/write, read-only, or write-only • Properties - Simple, which means it contains a single value, or indexed, which means it represents an array of values • For each readable property, the bean must have a method of the form: PropertyClass getProperty() { ... } • 2 syntaxes - standard and XML • Life Cycle • Translation and Compilation • Execution Dickson Chiu 2004
JSP Translation & Compilation • Directives are used to control how the Web container translates and executes the JSP page. • Scripting elements are inserted into the JSP page's servlet class. • Expression language expressions are passed as parameters to calls to the JSP expression evaluator. • jsp:[set|get]Property elements are converted into method calls to JavaBeans components. • jsp:[include|forward] elements are converted into invocations of the Java Servlet API. • The jsp:plugin element is converted into browser-specific markup for activating an applet. • Custom tags are converted into calls to the tag handler that implements the custom tag. Dickson Chiu 2004
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <html> <head><title>Hello</title></head> <body bgcolor="white"> <img src="duke.waving.gif"> <h2>My name is Duke. What is yours?</h2> <form method="get"> <input type="text" name="username" size="25"> <p></p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> <jsp:useBean id="userNameBean" class="hello.UserNameBean" scope="request"/> <jsp:setProperty name="userNameBean" property="name" value="${param.username}" /> <c:if test="${fn:length(userNameBean.name) > 0}" > <%@include file="response.jsp" %> </c:if> </body> </html> <html xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" > <head><title>Hello</title></head> <body bgcolor="white" /> <img src="duke.waving.gif" /> <h2>My name is Duke. What is yours?</h2> <form method="get"> <input type="text" name="username" size="25" /> <p></p> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </form> <jsp:useBean id="userNameBean" class="hello.UserNameBean" scope="request"/> <jsp:setProperty name="userNameBean" property="name" value="${param.username}" /> <c:if test="${fn:length(userNameBean.name) gt 0}" > <jsp:directive.include="response.jsp" /> </c:if> </body> </html> JSP Example – standard vs XML syntax Dickson Chiu 2004
Bonus – JavaBean Brief Overview • A constructor that takes no parameters • Properties • Read/write, read-only, or write-only • Simple, which means it contains a single value, or indexed, which means it represents an array of values • For each readable property, the bean must have a method of the form: • PropertyClass getProperty() { ... } • For each writable property, the bean must have a method of the form • setProperty(PropertyClass pc) { ... } Dickson Chiu 2004
Advantages of using the XML syntax • You can author a JSP document using one of the many XML-aware tools on the market, enabling you to ensure that your JSP document is well-formed XML. • You can validate the JSP document against a document type definition (DTD). • You can nest and scope namespaces within a JSP document. • You can use a JSP document for data interchange between Web applications Dickson Chiu 2004
“Book.jspx” XML Example <books xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" > <jsp:useBean id="bookDB" class="database.BookDB" scope="page" > <jsp:setProperty name="bookDB" property="database" value="${bookDBAO}" /> </jsp:useBean> <c:forEach var="book" begin="0" items="${bookDB.books}"> <book id="${book.bookId}" > <surname>${book.surname}</surname> <firstname>${book.firstName}</firstname> <title>${book.title}</title> <price>${book.price}</price> <year>${book.year}</year> <description>${book.description}</description> <inventory>${book.inventory}</inventory> </book> </c:forEach> </books> Dickson Chiu 2004