JSP

JSP are Java's answer to Active Server Pages. They let you create dynamic Web content in much the same way, by running scripts on the server. You can read all about them at the main JavaServer Pages page, which, as of this writing, is at http://java.sun.com/products/jsp/index.html. Using JSP is fairly close to using ASP.

In this example, I'm going to use the Tomcat server, which we saw in Chapter 18 and which is the official reference implementation for JSP (and Java servlets, for that matter). You can download Tomcat at the Tomcat main page, currently at http://jakarta.apache.org/tomcat/ (see Chapter 18 for more on installing this Web server locally).

I'll create the same XML document as in the previous two examples here, by searching ch20_01.mdb for all students and returning them in an XML document. Because we're working in Java again here, I'll use JDBC to connect to ch20_01.mdb.

Once again, a major point here is to make sure that the content type of the document we send to the client is "application/xml" , not the default HTML type. In JSP, you do that with the contentType attribute of the page directive like this:

 <%@ page language="java" contentType="application/xml"      import="java.sql.*" %>     .     .     . 

I also initialize the JDBC driver like this:

 <%@ page language="java" contentType="application/xml"      import="java.sql.*" %>  <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; %>  .     .     . 

And I send the XML declaration and the document element back to the client like this:

 <%@ page language="java" contentType="application/xml"      import="java.sql.*" %> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; %>  <?xml version="1.0"?>   <document>  .     .     . 

Now I get a JDBC result set with all the students' records using a SQL statement:

 <%@ page language="java" contentType="application/xml"      import="java.sql.*" %> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; %> <?xml version="1.0"?> <document> <% Connection connection = DriverManager.getConnection(     "jdbc:odbc:students", "Steve", "password");  Statement statement = connection.createStatement() ;   ResultSet resultset =   statement.executeQuery("select * from Students") ; %>  .     .     . 

All that's left is to loop over the students' records and send the matching <student> elements back to the client:

Listing ch20_04.jsp
 <%@ page language="java" contentType="application/xml"     import="java.sql.*" %> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; %> <?xml version="1.0"?> <document> <% Connection connection = DriverManager.getConnection(     "jdbc:odbc:students", "Steve", "password"); Statement statement = connection.createStatement() ; ResultSet resultset =     statement.executeQuery("select * from Students") ; %>  <% while(resultset.next()){ %>   <student> <%= resultset.getString(1)  %>  </student>   <% } %>   </document>  

And that's it. You can see the results of this JSP script in Figure 20-3. As you can see, JSP works as well as ASP when it comes to serving XML documents. In fact, I know plenty of XML developers that prefer JSP over ASP for this purpose because working with XML using Java is a natural, as we've seen in Chapter 11, "Java and the XML DOM," and Chapter 12, "Java and SAX."

Figure 20-3. A JSP and XML page.

graphics/20fig03.gif



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net