XML and Java Servlets

Java servlets are supposed to be 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 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) and create servlets using the classes in servlet.jar and server.jar.

To read the names of the students from db.mdb, I'll use the Java Database Connectivity package (JBDC), interfacing to db.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 create an XML document, not the default HTML document. In servlets, you create an XML document with the ServletResponse class's setContentType method, setting the content type to "application/xml" like this:

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 db.mdb, using an 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:

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) + "</student>");
             }
        }
        catch(Exception e) {}
        
        printwriter.println("</document>");
        printwriter.close();
    }
}

And that's all it takes. This servlet is running in Figure 20.2, where the same XML document discussed in the preceding section is delivered to the client.

Figure 20.2. Creating XML documents with Java servlets.


Another Java technology that's becoming popular for serving XML documents is Java Server Pages, as discussed in the next section.

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

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