Why is my Java JOX parser servlet failing to convert from bean to string?
Currently working through a tutorial on JOX included within Sams Teach Yourself JavaServer Pages 2.0 with Apache Tomcat in 24 Hours
Either the com.wutka.jox has changed or the author missed a crucial ingredient.
When I try to execute:
java "hu/flux/xml/XMLTestClient" "http://localhost:8080/SamsTeachYourselfJSP/joxparse" "d:\education\java\SamsTeachYourselfJSP\WebContent\test.xml"
My CMD console displays:
Sending: d:\education\java\SamsTeachYourselfJSP\WebContent\test.xml
To: http://localhost:8080/SamsTeachYourselfJSP/joxparse
Received:
null
And my Eclipse console displays:
Can't convert bean to String: null
java.lang.NullPointerException
at hu.flux.xml.TestBean.toString(TestBean.java:144)
at hu.flux.xml.JOXParseServlet.doPost(JOXParseServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at hu.flux.logging.SessionRecorder.doFilter(SessionRecorder.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at hu.flux.timing.TimingFilter.doFilter(TimingFilter.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
开发者_如何学运维 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Returning String: null
Here is the current code for the servlet:
package hu.flux.xml;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.wutka.jox.*;
import com.wutka.jox.test.*;
/**
* Servlet implementation class JOXParseServlet
*/
@SuppressWarnings("unused")
@WebServlet("/JOXParseServlet")
public class JOXParseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JOXParseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
com.wutka.jox.test.TestBean newBean = new com.wutka.jox.test.TestBean();
BufferedReader requestReader = request.getReader();
JOXBeanReader reader = new JOXBeanReader(requestReader);
reader.readObject(newBean);
// This servlet just sends a plain text response.
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String beanString = null;
try { beanString = newBean.toString(); }
catch (Exception exc)
{
System.err.println("Can't convert bean to String: " + exc.getMessage());
exc.printStackTrace();
}
System.out.println ("Returning String: " + beanString);
out.println(beanString);
}
}
Neither the XMLTestClient, nor test.xml are likely to be the problem, as they both work successfully with a Sax parser and a Dom parser.
However, in case it is useful or interesting to see them, here they are:
<!-- test.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<name>
<first>Brian</first>
<last>Kessler</last>
</name>
////////////////////////////////////////////
/**
* XMLTestClient.java
*/
package hu.flux.xml;
import java.io.*;
import java.net.*;
/**
* @author Brian Kessler
*
*/
public class XMLTestClient {
/**
*
*/
public XMLTestClient() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args)
{
try
{
// args[1] is the name of the file to send.
System.out.println("\n\nSending: " + args[1]);
System.out.println("To: " + args[0]);
System.out.println("\nReceived:\n\n");
try
{
File f = new File(args[1]);
int contentLength = (int) f.length();
// args[0] is the URL to send the file to.
URL url = new URL(args[0]);
URLConnection conn = url.openConnection();
// Tell the URLConnection that this is an XML file.
conn.setDoOutput(true);
conn.setRequestProperty("content-type", "text/xml");
conn.setRequestProperty("content-length", ""+contentLength);
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[4096];
int len;
OutputStream out = conn.getOutputStream();
// Send the XML file to the servlet.
while ((len = in.read(buffer)) > 0)
{ out.write(buffer, 0, len); }
InputStream resp = conn.getInputStream();
// Read the response back from the servlet.
while ((len = resp.read(buffer)) > 0)
{ System.out.write(buffer, 0, len); }
}
catch (Exception exc)
{
System.err.println("Missing file argument: " + exc);
exc.printStackTrace();
}
}
catch (Exception exc) { exc.printStackTrace(); }
}
}
The problem was with TestBean.
In the context of the book, it seemed that this could be some sort of generic bean.
In fact, the getters and setters in the bean need to match the contents of the XML.
The toString() failed because the bean wasn't being populated with data.
Once I created a bean to match the XML and included a custom toString(), I was able to get this servlet working. :-)
Here is a working bean:
/**
*
*/
package hu.flux.user;
/**
* @author Brian Kessler
*
*/
public class NamedPerson extends Person
{
private String firstName = "";
private String lastName = "";
public String getFirst() { return this.firstName; }
public void setFirst(String firstName) {this.firstName = firstName;}
public String getLast() { return this.lastName; }
public void setLast(String lastName) {this.lastName = lastName;}
public String toString() {return (this.firstName + " " + this.lastName);}
}
Also, in the servlet, I needed to change:
com.wutka.jox.test.TestBean newBean = new com.wutka.jox.test.TestBean();
to
Person newBean new Person();
Cheers to Teja Kantamneni for putting me on the track to the right answer. :-)
精彩评论