XML and Java Servlets

As you saw in Chapter 18, "SOAP and RDF," Java servlets are to Web servers what Java applets are to Web clients . You can use servlets to create Web documents on suitably enabled servers. To create servlets, you'll need servlet.jar, which comes with the Tomcat Web server used in Chapter 18, or you can download the Java Servlet Development Kit (JSDK) from java.sun.com (as of this writing, the main page for servlets is http://java.sun.com/products/servlet/index.html).

To read the names of the students from ch20_01.mdb, I'll use the Java Database Connectivity package (JBDC), interfacing to ch20_01.mdb after registering that database as an Open Database Connectivity (ODBC) data source. After searching for all the students, I'll return their names in an XML document and send that document back to the client.

Again, the key here is to make sure we create an XML document, not the default HTML document. You do that in servlets with the ServletResponse class's setContentType method, setting the content type to "application/xml" . Here's what that looks like:

 import java.net.*;  import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.servlet.*; public class xml extends GenericServlet {     public void service(ServletRequest request, ServletResponse         response) throws ServletException, IOException     {  response.setContentType("application/xml");  .     .     . 

Next , I send the XML declaration and document element back to the client:

 import java.net.*;  import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.servlet.*; public class xml extends GenericServlet {     Connection connection;     Statement statement;     public void service(ServletRequest request, ServletResponse         response) throws ServletException, IOException     {         response.setContentType("application/xml");  PrintWriter printwriter = response.getWriter();   printwriter.println("<?xml version=\"1.0\"?>");   printwriter.println("<document>");  .     .     . 

At this point, I can use JDBC to create a result set with all records from ch20_01.mdb. I do that like this with a SQL statement:

 import java.net.*;  import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.servlet.*; public class xml extends GenericServlet {     Connection connection;     Statement statement;     public void service(ServletRequest request, ServletResponse         response) throws ServletException, IOException     {         response.setContentType("application/xml");         PrintWriter printwriter = response.getWriter();         printwriter.println("<?xml version=\"1.0\"?>");         printwriter.println("<document>");  try   {   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   connection = DriverManager.getConnection(   "jdbc:odbc:students", "Steve", "password");   statement = connection.createStatement();   String SQL = "SELECT Name FROM Students";   ResultSet resultset = statement.executeQuery(SQL);  .     .     . 

All that's left is to loop over the result set with the next method, getting the student names and sending them back to the client in <student> elements like this:

Listing ch20_03.java
 import java.net.*; import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.servlet.*; public class xml extends GenericServlet {     Connection connection;     Statement statement;     public void service(ServletRequest request, ServletResponse         response) throws ServletException, IOException     {         response.setContentType("application/xml");         PrintWriter printwriter = response.getWriter();         printwriter.println("<?xml version=\"1.0\"?>");         printwriter.println("<document>");         try         {               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");               connection = DriverManager.getConnection(                   "jdbc:odbc:students", "Steve", "password");               statement = connection.createStatement();               String SQL = "SELECT Name FROM Students";               ResultSet resultset = statement.executeQuery(SQL);  while (resultset.next()) {   printwriter.println("<student>" + resultset.getString(1) + "</ graphics/ccc.gif student>");   }   }   catch(Exception e) {}   printwriter.println("</document>");   printwriter.close();   }   }  

And that's all it takes. You can see this servlet running in Figure 20-2, where you see the same XML document delivered to the client as we saw in the previous topic.

Figure 20-2. Creating XML documents with Java servlets.

graphics/20fig02.gif

Another Java technology is becoming popular for serving XML documents: JavaServer Pages.



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