Because of their flexibility, you can use JSPs and servlets to produce, consume or transform XML data. This section shows you how to get XML from your web components.
To produce XML from a JSP, set the contentType attribute of the page directive to text/xml. The following example shows how to set contentType to text/xml in a JSP:
<%@ page contentType="text/xml" %>
<?xml version="1.0" encoding="ISO-8859-1"?> <movie name="Planet of the Apes"> <actor> <f_name>Charleton</f_name> <l_name>Heston</l_name> <quote>You damned dirty ape</quote> </actor> <rating body="MPAA">R</rating> </movie>
To see a sample JSP, start the samples JRun server and open a browser to http://localhost:8200/techniques.
Producing XML from a JSP allows components to use XSLT to transform the page contents or SAX/DOM to convert the XML into server-side objects and extract the page's properties.
When invoked in a browser that supports XML (such as Microsoft's Internet Explorer), the previous code sample displays the following:
Note: XML syntax does not allow whitespace or line breaks before the <?xml version="1.0" encoding="ISO-8859-1"?>. Internet Explorer allows this when it parses XML. Other browsers might not.
You can also dynamically produce XML from data in JSPs by adding HTML-like tags in the appropriate places while you iterate over a data source.
The following code sample shows you how to send the data from the movies table in the Samples datasource to XML format:
<%@ page contentType="text/xml" %>
<%@ page import="java.sql.*,javax.naming.*,javax.sql.*" %>
<%
�String sqlstmt="SELECT movie FROM movies";
�String dsName="Samples";
�InitialContext ctx=new InitialContext();
�DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/" + dsName);
�Connection myConn=ds.getConnection();
�Statement myStmt=myConn.createStatement();
�ResultSet myRs=myStmt.executeQuery(sqlstmt);
%>
<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<% while (myRs.next()) { %>
�<movie><%=myRs.getString("movie") %></movie>
�<rating><%=myRs.getString("rating") %></rating>
<% } %>
</document>
The previous code sample produces output similar to the following:
<document>
��<movie>Planet of the Apes</movie> ��<rating>R</rating> </document>
In addition to the traditional JSP syntax used in the previous code sample, JSPs can also be written in JSP XML syntax. For more information, see "Writing JSPs in XML".
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
Servlets use similar techniques to JSPs for producing XML. The most important difference is that you use the HttpServletResponse object's setContentType method, rather than the page directive to set the setContentType to text/xml. You must also escape the quotation characters used in the XML elements.
You must call setContentType before you call getWriter. You can set the encoding in the same method call. You can use println statements to extract XML data from a servlet, as shown in the following example:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MovieServlet extends HttpServlet {
�public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
��response.setContentType("text/xml;charset=ISO-8859-1");
��PrintWriter out = response.getWriter();
��out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
��out.println("<movie name=\"Planet of the Apes\">");
��out.println("<actor><f_name>Charleton</f_name>");
��out.println("<l_name>Heston</l_name>");
��out.println("<quote>You damned dirty ape</quote>");
��out.println("</actor><rating body=\"MPAA\">R</rating></movie>");
�}
}
The resulting output in your web browser appears the same as that produced by the JSP example described in "Producing XML from a JSP". You can also export a page that contains only HTML tags with a content type of text/xml. However, you must use well-formed HTML. You do this by properly closing all tags, even <P> and <BR> tags to avoid XML validation errors.
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/servletxml4.htm