Jsp and XML

Reply

Join Date: Sep 2006
Posts: 12
Reputation: sathya_k_83 is an unknown quantity at this point 
Solved Threads: 1
sathya_k_83 sathya_k_83 is offline Offline
Newbie Poster

Jsp and XML

 
0
  #1
Sep 22nd, 2007
i am developing a project using jsp & servlet.
i want create a xml from result set using document builder but when the servlet is executed it displays the Http Status 500 error

tell me what to do . where to place that xml file

is there other for writing result set data as xml
below is the code that does not work

  1. protectedvoid doPost(HttpServletRequest request,
  2. HttpServletResponse response) throws ServletException, IOException
  3. {
  4. // TODO Auto-generated method stub
  5. Document mapDoc;
  6. Document dataDoc = null;
  7. String XmlFileName = request.getParameter("filename");
  8. DocumentBuilder db;
  9. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  10. System.out.println(XmlFileName);
  11. try
  12. {
  13. db = dbf.newDocumentBuilder();
  14. mapDoc = db.parse(XmlFileName);
  15. dataDoc = db.newDocument();
  16. }
  17. catch (ParserConfigurationException e)
  18. {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. catch (SAXException e)
  23. {
  24. // TODO Auto-generated catch block
  25. System.out.println("sax");
  26. e.printStackTrace();
  27. }
  28. catch (IOException e)
  29. {
  30. // TODO Auto-generated catch block
  31. System.out.println("IO ->");
  32. e.printStackTrace();
  33. }
  34. Connection con;
  35. Statement st;
  36. ResultSet rs;
  37. ResultSetMetaData rsd = null;
  38. Element DocRoot = null;
  39. String query = "select * from StationarRequest";
  40. try
  41. {
  42. Class.forName(DbConnect.getDriverName());
  43. con = DriverManager.getConnection(DbConnect.getDsnName(), DbConnect
  44. .getUid(), DbConnect.getPwd());
  45. st = con.createStatement();
  46. rs = st.executeQuery(query);
  47. rsd = rs.getMetaData();
  48. DocRoot = dataDoc.createElement("StationaryDb");
  49. while (rs.next())
  50. {
  51. Element row = dataDoc.createElement("Stationary");
  52. for (int i = 0; i < rsd.getColumnCount(); i++)
  53. {
  54. String colName = rsd.getColumnName(i);
  55. String colType = rsd.getColumnTypeName(i);
  56. System.out.println(colType);
  57. String colValue = "";
  58. if (colType.equals("String"))
  59. {
  60. colValue = rs.getString(i);
  61. }
  62. else if (colType.equals("int"))
  63. {
  64. colValue = rs.getInt(i) + "";
  65. }
  66. else if (colType.equals("float"))
  67. {
  68. colValue = rs.getFloat(i) + "";
  69. }
  70. else if (colType.equals("double"))
  71. {
  72. colValue = rs.getDouble(i) + "";
  73. }
  74. Element DataCol = dataDoc.createElement(colName);
  75. DataCol.appendChild(dataDoc.createTextNode(colValue));
  76. row.appendChild(DataCol);
  77. }
  78. DocRoot.appendChild(row);
  79. }
  80. }
  81. catch (ClassNotFoundException e)
  82. {
  83. // TODO Auto-generated catch block
  84. System.out.println("class");
  85. e.printStackTrace();
  86. }
  87. catch (SQLException e)
  88. {
  89. // TODO Auto-generated catch block
  90. System.out.println("sql");
  91. e.printStackTrace();
  92. }
  93. dataDoc.appendChild(DocRoot);
  94. System.out.println("The Result Set is written into XML File");
  95. }
Last edited by ~s.o.s~; Sep 22nd, 2007 at 10:40 am. Reason: Added code tags and formatting. Learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Jsp and XML

 
0
  #2
Sep 22nd, 2007
Hey 500 means internal server error. please check the logs and then send the exact error/exceptions. 500 can occur due to n reasons.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Jsp and XML

 
0
  #3
Sep 23rd, 2007
not n reasons, there are at least n+1 reasons!
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 12
Reputation: sathya_k_83 is an unknown quantity at this point 
Solved Threads: 1
sathya_k_83 sathya_k_83 is offline Offline
Newbie Poster

Re: Jsp and XML

 
0
  #4
Sep 24th, 2007
The jsp passes a filename and the servlet reads data from the Database
and stores the result set data in the xml file (the file name is passed from the jsp page)

The servlet cannot find my xml file
and displays the following error while trying to execute the servlet

type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.

exception java.lang.NullPointerException aspire.controller.WriteXml.doPost(WriteXml.java:125) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
Apache Tomcat/5.5.20

The below output appers in the console when the servlet is executed

java.io.FileNotFoundException: D:\Eclipse3.2\eclipse wtp\Sample.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.http://www.protocol.file.FileURLConn...onnect(Unknown Source)
at sun.net.http://www.protocol.file.FileURLConn...Stream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at aspire.controller.WriteXml.doPost(WriteXml.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
sql
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'StationarRequest'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at aspire.controller.WriteXml.doPost(WriteXml.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Sep 24, 2007 9:14:32 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet WriteXml threw exception
java.lang.NullPointerException
at aspire.controller.WriteXml.doPost(WriteXml.java:125)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)

i need to know where the xml file is to be placed so the servlet will be able to access it and write the data into it.

Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 12
Reputation: sathya_k_83 is an unknown quantity at this point 
Solved Threads: 1
sathya_k_83 sathya_k_83 is offline Offline
Newbie Poster

Found the solutiion

 
0
  #5
Sep 24th, 2007
i have used FileOutputStream and PrintWriter to create a xml file

i am using eclipse and the file gets stored in the eclipse's bin directory

the jsp page passes a file name to a servlet

and the servlet uses the FileOutputStream and PrintWriter to write the data from
Result Set using the below code


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//Document mapDoc;
//Document dataDoc=null;
String XmlFileName=request.getParameter("filename");
System.out.println(XmlFileName);
FileOutputStream fout;
PrintWriter pw;
response.setContentType("text/html");
PrintWriter out=response.getWriter();


fout=new FileOutputStream(XmlFileName);

pw=new PrintWriter(fout);

Connection con;
Statement st;
ResultSet rs;
ResultSetMetaData rsd=null;
//Element DocRoot=null;
String query="select * from StationaryRequest";
try {
Class.forName(DbConnect.getDriverName());
con=DriverManager.getConnection(DbConnect.getDsnName(),DbConnect.getUid(),DbConnect.getPwd());
st=con.createStatement();
rs=st.executeQuery(query);
rsd=rs.getMetaData();
// DocRoot=dataDoc.createElement("StationaryDb");// add a docu root
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pw.println("<StationaryDb>");
out.println("<StationaryDb>");
while(rs.next())
{
//Element row=dataDoc.createElement("Stationary");
pw.println("<Stationary>");
out.println("<Stationary>");
for(int i=1;i<=rsd.getColumnCount();i++)
{
String colName=rsd.getColumnName(i);
String colType=rsd.getColumnTypeName(i);
System.out.println(colType);

String colValue="";
if(colType.equals("varchar"))
{
colValue=rs.getString(i);
}
else if(colType.equals("int") ||colType.equals("statLogId"))
//here statLogId is a userdefined data type created in sql server
{
colValue=rs.getInt(i)+"";
}
else if(colType.equals("float"))
{
colValue=rs.getFloat(i)+"";
}
else if(colType.equals("double"))
{
colValue=rs.getDouble(i)+"";
}

//Element DataCol=dataDoc.createElement(colName);//write a columnname
pw.println("<"+colName+">");
out.println("<"+colName+">");

//DataCol.appendChild(dataDoc.createTextNode(colValue));//write a value to the column
pw.println(""+colValue+"");
out.println(""+colValue+"");

pw.println("</"+colName+">");
out.println("</"+colName+">");

//row.appendChild(DataCol); //adding the column to a row
}

//DocRoot.appendChild(row); // adding a row to a xml parent root
pw.println("</Stationary>");
out.println("</Stationary>");
}


} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("class");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("sql");
e.printStackTrace();
}


//dataDoc.appendChild(DocRoot);
pw.println("</StationaryDb>");
out.println("</StationaryDb>");
pw.close();
fout.flush();
fout.close();
System.out.println("The Result Set is written into XML File");
}
Last edited by sathya_k_83; Sep 24th, 2007 at 1:43 am. Reason: change in the code
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Jsp and XML

 
0
  #6
Sep 24th, 2007
r u using JDBC ODBC bridge for establishing Connection with MS SQL Server. If that's the case, then you need to modify ur web.xml file in WEB-INF folder. The Context tag should be something like the one given below:
  1. <<Context docBase="Wims" path="/Wims" reloadable="true" source="org.eclipse.jst.j2ee.server:Wims">
  2. <Resource name="jdbc/WimsDs" auth="Container" type="javax.sql.DataSource"
  3. factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  4. maxActive="100" maxIdle="30" maxWait="10000"
  5. username="" password="" driverClassName="sun.jdbc.odbc.JdbcOdbcDriver"
  6. url="jdbc:odbc:WimsDs"
  7. removeAbandoned="true"
  8. removeAbandonedTimeout="60"
  9. logAbandoned="true"/>
  10. </Context>

You'll have to replace the Wims with your Specific requirements. And you'll have to figure it out what you need to change. I was using javax.sql.DataSource here to connect to MS Access. If you're using DriverManager then probably you've to research more....
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Jsp and XML

 
0
  #7
Sep 24th, 2007
Also, you will require resource ref tags. Please put them in proper order otherwise some exception might occur.

  1. <resource-ref>
  2. <res-ref-name>jdbc/WimsDs</res-ref-name>
  3. <res-type>javax.sql.DataSource</res-type>
  4. <res-auth>Container</res-auth>
  5. <res-sharing-scope>Unshareable</res-sharing-scope>
  6. </resource-ref>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC