- From: Ying Xie <xie@freedom.ncsa.uiuc.edu>
- Date: Thu, 25 Jul 1996 00:13:17 -0500
- To: www-jigsaw@w3.org
- Cc: xie@freedom.ncsa.uiuc.edu
I changed the following code a little bit and couldn't understand the behavior (the code was posted before and worked all right without the change): ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ resource for jigsaw: //ReverseString.java: package w3c.jigsaw.contrib; import java.util.*; import w3c.jigsaw.http.*; import w3c.jigsaw.forms.*; public class ReverseString extends PostableResource { public Reply handle(Request request, URLDecoder data) throws HTTPException { String str = data.getValue("string"); if ((str == null) || str.equals("")) { Reply reply = request.makeReply(HTTP.OK); reply.setContent("reverse=[]"); return reply; } // Reverse the string (should be some other method, etc) // You could ameliroate this alot, but that's not the point char cs[] = new char[str.length()]; str.getChars(0, cs.length, cs, 0); char cr[] = new char[cs.length]; for(int i = 0 ; i < cs.length; i++) cr[cs.length-1-i] = cs[i]; // Emit reply: Reply reply = request.makeReply(HTTP.OK); ----------> reply.setContent("reverse=["+new String(cr)+"]"); return reply; } public ReverseString() { } } *** the above is original code. If I delete line pointed by "--->" and put in these lines: FileInputStream f = new FileInputStream("tmpfile"); reply.setStream(f); Of course it no longer does reverse string. The new resource after the change takes a request and then send the content of tmpfile back to the client. I tested the new resource with netscape by http://.../reverse?string="jjj" and it worked all right. But if I use the following client program (which I didn't change and worked great with the original resource), an Exception will be thrown at the line pointed by "------------>", i.e., connection.getInputStream(). on the client side: //ReverseTest.java import java.io.*; import java.net.*; public class ReverseTest { public static void main(String args[]) { try { if (args.length != 1) { System.err.println("Usage: java ReverseTest string_to_reverse"); System.exit(1); } String stringToReverse = URLEncoder.encode(args[0]); // Put you URL here !!! URL url = new URL("http://cmt.physics.ucf.edu:9999/User/reverse"); System.out.println("protocol = " + url.getProtocol()); System.out.println("host = " + url.getHost()); System.out.println("filename = " + url.getFile()); System.out.println("port = " + url.getPort()); System.out.println("ref = " + url.getRef()); URLConnection connection = url.openConnection(); // writing to URL PrintStream outStream = new PrintStream(connection.getOutputStream()); DataInputStream inStream; String inputLine; outStream.println("string=" + stringToReverse); outStream.close(); // reading from URL ------------------> inStream = new DataInputStream(connection.getInputSt ream()); while (null != (inputLine = inStream.readLine())) { System.out.println(inputLine); } inStream.close(); } catch (MalformedURLException me) { System.err.println("MalformedURLException: " + me); } catch (IOException ioe) { System.err.println("IOException: " + ioe); } } } ================================================== I tried to seperate the line pointed by "------>" into two statements and made sure that the exception was thrown at connection.getInputStream(). It was a java.lang.NumberFormatException. How could this happen? Any hint? Thanks, Ying
Received on Thursday, 25 July 1996 01:13:24 UTC