Java Server Pages

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

In this example, I'll use the Apache Tomcat server, 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/.

I'll create the same XML document as in the previous two examples by searching db.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 db.mdb.

Again, a major point 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 (this one took me a while to find out), 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") ; %>
    .
    .
    .

Then 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 by using an 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:

<%@ 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>

Figure 20.3 shows the results of this JSP script. As the figure shows, JSP works as well as ASP when serving XML documents. In fact, I know plenty of XML developers who prefer JSP over ASP for this purpose because working with XML using Java is so natural, as I've shown in Chapters 11, "Java and the XML DOM," and 12, "Java and SAX."

Figure 20.3. Creating XML documents with Java Server Pages.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.115.202