Serlvet/Xml Optional Assignment FAQ

 

1. What stylseheets should I use?

Use the stylsheets in the cs433 course directory on Goose.  The styesheets you need are in stylesheets\client.  You will want to rename some of the stylesheets to match the assignment:

            AWtoORQuery.xsl            à            AWtoORRequest.xsl

            AWtoPHQuery.xsl            à            AWtoPHRequest.xsl

 

 

2. Why doesn’t the server work?

You have to start the server on your local machine. You should take a look at the file Supplier.java to understand what is going on.

 

3. How do I use Sockets?

You must use sockets to connect to the Supplier server.  It is VERY important that you close the socket every time you use one, regardless of whether an exception was thrown or not.  To do this properly, you can use this code:

 

private String SocketConnection(String host, int port, String query)

       {

              String result = "";

              Socket s = null;

              try

              {

                     s = new Socket(host, port);

                    

                     // send a string to the server

                     // the string you send MUST NOT have any carriage returns or line breaks

                     PrintStream serverOut = new PrintStream(s.getOutputStream());

                     serverOut.println(query);

                     serverOut.flush();                // use flush() to send the string

                    

                     // get the response from the server

                     InputStream in = s.getInputStream();

                     BufferedReader d = new BufferedReader(new InputStreamReader(in));

                     result = d.readLine();

                    

              }

              catch(Exception e)

              {

                     result = e.toString();

              }

              finally

              {

                     // a finally block ensures that the socket will be closed

                     try

                     {

                            if(s != null)

                                   s.close();

                     }

                     catch(Exception e2)

                     {

                            result = e2.toString();

                     }

              }

              return result;

       }

 

4. How to I traverse an Xml tree to find out what is in the Xml returned from the Supplier server?

You can use the Supplier.java code as a reference to how to do this.  Also, refer to the API of the Xml tools for Xerces and Xalan.  You will need these headers to traverse an Xml tree:

import org.apache.xerces.parsers.*;

import org.w3c.dom.*;

import org.w3c.dom.traversal.*;

import org.xml.sax.ErrorHandler;

import org.xml.sax.SAXParseException;

 

5. When should I do the transformation?

You should do the transformation BEFORE opening a socket connection.  If your code throws an exception while doing the transformation, then you will have wasted a socket connection.

 

Suggestions:

 

Figure out how to do the transformations first.  You should write code that does the transformation, catches any exceptions, removes any carriage returns, and prints the transformed Xml to the screen.  If you can’t see your output, try to “View Source” for the web page that is returned (since Internet Explorer will interpret the Xml tags as Html and might hide them).

 

Once (and only once) all of your transformations work properly, use the Socket code to communicate with the server.

 

After you receive the correct responses from the server, you can parse the results and finish your project.