Getting an application and servlet to talk

** SOLVED **

When I run the servlet from my browser, I get the output "command not known" which means the servlet is outputting text correctly. Presumably since it got that far without giving me an exception, it 'succeded' in reading input.

When I run the application, the output I get is... not text and does not seem to change when I change the command. But no exceptions ther either.

So I'm losing something in one of the class conversions somewhere...

Where?

If I change the command, the output does not change. Does this mean I'm not getting the command from the application to the servlet? Please advise!

Servlet:

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

publicclass Serverextends HttpServlet

{

/**

* Servlet: Get input string from request, parse and send reply string to response

*/

privatestaticfinallong serialVersionUID = -767626582925914901;

protectedvoid service(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{

byte[] input =newbyte[50];

ServletInputStream in = req.getInputStream();

in.readLine(input, 0, 50);

ServletOutputStream out = res.getOutputStream();

if (in.toString().equals("hi"))

out.print("hello");

if (in.toString().equals("bye"))

out.print("have a good day");

else

out.print("command not known");

out.close();

}

}

Application:

import java.io.*;

import java.net.*;

publicclass Client{

/**

* Connect to servlet, send message, receive + print response

*/

publicstaticvoid main(String[] args)throws Exception{

Exception toss;

// Create URL

URL u;

try{

u =new URL("http://localhost:8080/war/Server");

}catch (MalformedURLException e){

System.out.println("Bad URL");

toss =new Exception("Bad URL");

throw(toss);

}

// Connect to the URL (servlet)

HttpURLConnection con;

try{

con = (HttpURLConnection)u.openConnection();

con.setRequestMethod("GET");

con.setDoInput(true);

con.setDoOutput(true);

con.connect();

}catch (IOException e){

System.out.println("Can't open connection");

toss =new Exception("Can't open connection");

throw(toss);

}

// Write to the servlet

OutputStreamWriter out;

try{

out =new OutputStreamWriter(con.getOutputStream());

out.write("hi");

out.flush();

out.close();

}catch(IOException e){

System.out.println("Can't get output stream");

toss =new Exception("Can't get output stream");

throw(toss);

}

// Read response from servlet

InputStreamReader in;

try{

in =new InputStreamReader(con.getInputStream());

char[] input =newchar[50];

in.read(input);

System.out.print("Response: ");

System.out.println(input.toString());

}catch (IOException e){

System.out.println("Can't get input stream");

toss =new Exception("Can't get input stream");

throw(toss);

}

}

}

** SOLVED **

Message was edited by:

yitzle

[6233 byte] By [yitzle] at [2007-11-26 7:18:10]
# 1

Hi yitzle,

I know Rich replied to you on a separate post. I am including his response here so that other people reading this particular post will be aware of the Java Technology forums.

Thanks,

Chris

"Java programming questions might get more attention over in the Java Technology forums; at http://forum.java.sun.com/index.jspa -- this is the Java Enterprise System (Sun's bundle of server-side products like application server, messaging server, etc.)"

ckamps32 at 2007-7-6 18:56:44 > top of Java-index,Java Enterprise System,Java Enterprise System - General Discussion...