Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started 2.

Similar presentations


Presentation on theme: "1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started 2."— Presentation transcript:

1 1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started 2

2 Dickson Chiu 2004CSIT600b 2p-2 Assignment Overview XSLT experiment - interactive try and error with IE6 Existing HTML sites Jtidy Parser XSLT1 Info Summary XSLT2XSLT3 WML HTML WAP access Browser access HTML J2EE environment

3 Dickson Chiu 2004CSIT600b 2p-3 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/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

4 Dickson Chiu 2004CSIT600b 2p-4 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()); }

5 Dickson Chiu 2004CSIT600b 2p-5 Front Page Interface CSIT600B HTML to DOM Demo HTML to DOM Demo URL:

6 Dickson Chiu 2004CSIT600b 2p-6 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/res ult?url=http://www.ust.hk View the source and find it pretty- print with indentation

7 Dickson Chiu 2004CSIT600b 2p-7 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

8 Dickson Chiu 2004CSIT600b 2p-8 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.

9 Dickson Chiu 2004CSIT600b 2p-9 JSP Example – standard vs XML syntax <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> Hello My name is Duke. What is yours? <jsp:useBean id="userNameBean" class="hello.UserNameBean" scope="request"/> <jsp:setProperty name="userNameBean" property="name" value="${param.username}" /> 0}" > <html xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" > Hello My name is Duke. What is yours? <jsp:useBean id="userNameBean" class="hello.UserNameBean" scope="request"/> <jsp:setProperty name="userNameBean" property="name" value="${param.username}" />

10 Dickson Chiu 2004CSIT600b 2p-10 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) {... }

11 Dickson Chiu 2004CSIT600b 2p-11 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

12 Dickson Chiu 2004CSIT600b 2p-12 “Book.jspx” XML Example <books xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" > ${book.surname} ${book.firstName} ${book.title} ${book.price} ${book.year} ${book.description} ${book.inventory}


Download ppt "1 Dickson K.W. Chiu PhD, SMIEEE Reference: Sun J2EE 1.4 Tutorial CSIT600b: XML Programming XML Programming Guide: Getting Started 2."

Similar presentations


Ads by Google