Generating XML with JSP | ||||||||||||
XML can be generated on a server without any installed XML software. | ||||||||||||
To generate an XML response from the server – simply write the following code and save it as an JSP file on the Tomcat web server and create a System DSN named “simple” that will be used to connect the JSPto the database. | ||||||||||||
The Associate table contains the following fields: | ||||||||||||
|
||||||||||||
<% java.sql.ResultSet rs; java.sql.PreparedStatement pst; java.sql.Connection con; response.setContentType(“text/xml”); out.println(“<?xml version=\”1.0\” encoding=\”ISO-8859-1\”?>”); out.println(“<ebiz>”); out.println(“<details>”); try { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //Accessing jdbc driver in JSP con=java.sql.DriverManager.getConnection(“jdbc:odbc:simple”); //here simple is the name of DSN that is used to connect to desired database pst=con.preapareStatement(“select * from associate”); //here associate is the name of database table rs=pst.executeQuery(); if(rs.next()) { out.println(“<name>”+rs.getString(“name”)+”</name>”); out.println(“<age>”+rs.getInt(“age”)+”</age>”); out.println(“<address_str>”+rs.getString(“address”)+”</address_str>”); out.println(“<city>”+rs.getString(“city”)+”</city>”); out.println(“<state>”+rs.getString(“state”)+”</state>”); } } catch(Exception e) { out.println(“<name>Unable to retrieve details from server </name>”); } finally { out.println(“</details></ebiz>”); con.close(); } %> |