Plz discuss the followed issue about Servlet and HttpURL.........

Under tomcat platform, I wrote a servlet ,and it's functionality is get data stream from client and display in the console window.:

publicclass UploadFileServletextends HttpServlet

{

protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException

{

InputStream inf=request.getInputStream();

int len=0;

byte b[]=newbyte[20];

while((len=inf.read(b))>0)

{

String str=new String(b,0,len);

System.out.println(str);

}

}

}

And the followed is the client..

URL url=new URL("http://localhost:8080/Test");//这个url可以访问上面的Servlet

HttpURLConnection httpConnection=(HttpURLConnection) url.openConnection();

httpConnection.setRequestMethod("POST");

httpConnection.setDoOutput(true);

OutputStreamWriter osw=new OutputStreamWriter(httpConnection.getOutputStream());

osw.write("A is Writing Something");

osw.flush();

System.in.read();//wait for input for a stop.....

osw.write("\n Are you OK?");

osw.flush();

osw.close();

System.out.println(httpConnection.getContentType());

The server should display "A is Writing Something "...then wait for the client to input , then display "\nAre you OK? "...But here, it doesn't work !

Why ? and how to implement such a functionality ?

Thanks for your reading...

Message was edited by:

Seaughter

[2318 byte] By [Seaughtera] at [2007-10-2 20:33:21]
# 1

This should work.

By the way Ididnt test this there might be compilation errors

public class UploadFileServlet extends HttpServlet

{

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));

//Read the br

br.close();

//Send something to client

responce.setContentType("text/plain");

DataOutputStream dos = new DataOutputStream(responce.getOutputStream());

dos.writeBytes("DUMMY");

dos.close();

}

}

URL url=new URL("http://localhost:8080/Test");//这个url可以访问上面的Servlet

HttpURLConnection httpConnection=(HttpURLConnection) url.openConnection();

httpConnection.setDoOutput(true);

httpConnection.setRequestHeader("content-type","binarry/data");

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(httpConnection.getOutputStream()));

bw.write("A is Writing Something");

bw.write("\n Are you OK?");

bw.close();

httpConnection.setDoInput(true);

System.out.println(httpConnection.getContentType());

LRMKa at 2007-7-13 23:16:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...