Transmitting XML Data Between A Java Client and WebLogic Server

7 XML Programming Techniques 7-1 7 XML Programming Techniques The following sections provide information about specific XML programming techniques for developing a J2EE application that processes XML data: ■ Section 7.1, Transmitting XML Data Between A Java Client and WebLogic Server ■ Section 7.2, Handling XML Documents in a JMS Application ■ Section 7.3, Accessing External Entities That Do Not Have an HTTP Interface

7.1 Transmitting XML Data Between A Java Client and WebLogic Server

In a typical J2EE application, a client application sends XML data to a servlet or a JSP that processes the XML data. The servlet or JSP then either sends the data on to another J2EE component, such as a JMS destination or an EJB, or sends the processed XML data back to the client in the form of another XML document. To send XML data from a Java client to a WebLogic Server-hosted servlet or JSP which then returns the data to the client, use the java.net.URLConnection class. This class represents the communication link between an application and an URL, which in this case is the URL that invokes the servlet or JSP. Instances of the URLConnection class send the XML document using the HTTP POST method. The following Java client program from the WebLogic XML examples shows how to transmit XML data between the program and a JSP: import java.net.; import java.io.; import java.util.; public class Client { public static void mainString[] args throws Exception { if args.length 2 { System.out.printlnUsage: java examples.xml.Client URL Filename; } else { try { URL url = new URLargs[0]; String document = args[1]; FileReader fr = new FileReaderdocument; char[] buffer = new char[102410]; int bytes_read = 0; if bytes_read = fr.readbuffer = -1 { URLConnection urlc = url.openConnection; urlc.setRequestPropertyContent-Type,textxml; urlc.setDoOutputtrue; urlc.setDoInputtrue; 7-2 Programming XML for Oracle WebLogic Server PrintWriter pw = new PrintWriterurlc.getOutputStream; send xml to jsp pw.writebuffer, 0, bytes_read; pw.close; BufferedReader in = new BufferedReadernew InputStreamReaderurlc.getInputStream; String inputLine; while inputLine = in.readLine = null System.out.printlninputLine; in.close; } } catch Exception e { e.printStackTrace; } } } } The example shows how to open a URL connection to the JSP by using a URL from the argument list; obtain the output stream from the connection; and print the XML document provided in the argument list to the output stream, thus sending the XML data to the JSP. The example then shows how to use the getInputStream method of the URLConnection class to read the XML data that the JSAP returns to the client application. The following code segments from a sample JSP show how the JSP receives XML data from the client application, parses the XML document, and sends XML data back: BufferedReader br = new BufferedReaderrequest.getReader; DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance; DocumentBuilder db = fact.newDocumentBuilder; Document doc = db.parsenew InputSourcebr; ... PrintWriter responseWriter = response.getWriter; responseWriter.println?xml version=1.0?; ... For detailed information on programming WebLogic servlets and JSPs, see Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server.

7.2 Handling XML Documents in a JMS Application