[jdom-interest] JDOMServlet - Could someone cast an eye

JS smartt at dcs.kcl.ac.uk
Fri Apr 4 02:44:18 PST 2003


Hi there Group.
I was hoping someone would be able to help me with some code that Ive
knocked up to test JDOM being used in a servlet.Quick Synopsis:
Servlet gets hold of a stylesheet in its init() method and caches for
future reuse.The doGet() method then sets up a database connection, uses the
JDOMResultSetBuilder to get an XML representation of the record and uses
this as the XMLSource. A transformation is then done on the XMLSource and
I want this to be output to the browser for display.Everything compiles OK, I have setup tomcat, webxml, and jar files
appropriately and the Servlet runs using
http://127.0.0.1:8080/mywebapp/servlet/JDOMServlet.
Many thanks for taking the time to read this. Much appreciated.
Rgds

JS


I'm not sure how right my code is, but it runs with no output. Here's the
code......
import javax.servlet.*;
import javax.servlet.http.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import org.jdom.JDOMException;
import org.jdom.contrib.input.ResultSetBuilder;

import java.sql.*;
import java.io.*;
import java.io.IOException;
import java.lang.*;

public class JDOMServlet extends HttpServlet {

	private TransformerFactory tFactory = TransformerFactory.newInstance();
	private String url = "jdbc:odbc:StudentDB";
	private ResultSet rs = null;
	private StreamSource xsltSource;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		ServletContext context = config.getServletContext();
		try {
			//Want to cache the stylesheet for future resuse
			//then it doesnt have to be loaded constantly
			xsltSource = new
			StreamSource(context.getResourceAsStream("/Web-Inf/viewStudentDetails.xsl"));
//-------I think this is the problem line, it is LINE 35 ----------//
			Templates templates = tFactory.newTemplates(xsltSource);
		}
		catch (TransformerConfigurationException tce) {
			tce.printStackTrace();
		}

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, java.io.IOException {

		response.setContentType("text/html");

		//setup the database connection and use the ResultSetBuilder to generate
		//a JDOM XML document of a students details
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection conn = DriverManager.getConnection(url,"","");
			Statement stmt = conn.createStatement();
			System.out.println("Connected to database");

			Namespace ns = Namespace.getNamespace("xhtml", "http://w3.org/etc");

			rs = stmt.executeQuery("SELECT * FROM tblStudent WHERE StudentID =
			9917217");			System.out.println("Executing query:SELECT * FROM tblStudent WHERE
			StudentID = 9917217");			ResultSetBuilder builder = new ResultSetBuilder(rs);
			Document doc = builder.build();
			XMLOutputter xOutputter = new XMLOutputter("	", true);
			xOutputter.output(doc, System.out);

			//get the JDOM document that was produced by the JDOM ResultSetBuilder
			//and convert to a normal dom document for use by the transformer
			processor			org.jdom.output.DOMOutputter dOutputter = new
			org.jdom.output.DOMOutputter();			org.w3c.dom.Document domDocument = dOutputter.output(doc);
			Source xmlSource = new javax.xml.transform.dom.DOMSource(domDocument);

			//create the output result for the newly created XSLT document
			//this will allow me to output the result to the browser
			StreamResult result = new StreamResult response.getOutputStream());
			//get the transformer
			System.out.println("Preparing to create Transformer");
			Transformer transformer = tFactory.newTransformer(xsltSource);
			//Do the transform
			transformer.transform(xmlSource, result);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}

	}
}


Here's what the tomcat logfile had to say......

java.net.MalformedURLException
	at java.net.URL.<init>(URL.java:613)
	at java.net.URL.<init>(URL.java:476)
	at java.net.URL.<init>(URL.java:425)
	at
	org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:740)	at
	org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:685)	at
	org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:252)	at
	org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:503)	at
	org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:585)	at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:147)
	at
	org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1148)	at
	org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:983)	at JDOMServlet.init(JDOMServlet.java:35)
	at
	org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:919)	at
	org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:656)	at
	org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:401)	at
	org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at
	org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)	at
	org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)	at
	org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2349)	at
	org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:405)	at
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:376)	at
	org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:508)	at
	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:518)	at java.lang.Thread.run(Thread.java:536)
---------
java.net.MalformedURLException
	at java.net.URL.<init>(URL.java:613)
	at java.net.URL.<init>(URL.java:476)
	at java.net.URL.<init>(URL.java:425)
	at
	org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:740)	at
	org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:685)	at
	org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:252)	at
	org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:503)	at
	org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardParserConfiguration.java:585)	at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:147)
	at
	org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1148)	at
	org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:983)	at JDOMServlet.init(JDOMServlet.java:35)
	at
	org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:919)	at
	org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:656)	at
	org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:401)	at
	org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at
	org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)	at
	org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)	at
	org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2349)	at
	org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at
	org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at
	org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
	at
	org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)	at
	org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:405)	at
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:376)	at
	org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:508)	at
	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:518)	at java.lang.Thread.run(Thread.java:536)
Connected to database
Executing query:SELECT * FROM tblStudent WHERE StudentID = 9917217
<?xml version="1.0" encoding="UTF-8"?>
<result>
	<entry>
		<StudentID>9917217</StudentID>
		<DCSCentralID>1</DCSCentralID>
		<FirstName>Jason</FirstName>
		<LastName>Smartt</LastName>
		<DateOfBirth>10 June 1976 00:00:00 o'clock BST</DateOfBirth>
		<TermtimeAddress>25 Oakwood Close, Burton, London</TermtimeAddress>
		<TermtimePostCode>SE5 3LS</TermtimePostCode>
		<PermanentAddress>15 West Wickham Road, Bromley</PermanentAddress>
		<PermanentPostCode>BR4 5TT</PermanentPostCode>
		<PermanentPhoneNumber>02074556789</PermanentPhoneNumber>
		<MobileTelephone>07987 654664</MobileTelephone>
		<EmailAddress>smartt at dcs.kcl.ac.uk</EmailAddress>
		<EntryQualifications>AAC</EntryQualifications>
		<YearOfEntry>1999</YearOfEntry>
		<YearOfGrad>2003</YearOfGrad>
		<CurrentStage>3</CurrentStage>
		<CandidateNumber>E08956</CandidateNumber>
		<LecturerID>TC01</LecturerID>
		<UCASCode>G4N2</UCASCode>
		<Photograph />
	</entry>
</result>

Preparing to create Transformer
javax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerException: java.net.MalformedURLExceptionStopping service Internal Services
Stopping service Java Web Services Developer Pack






More information about the jdom-interest mailing list