dcsimg
EJB communicating with a servlet using a HttpURLConnection object
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, February 13, 2002 03:47 AM

I have an ejb communicating with a servlet. The ejb basically opens a URLConnection to the servlet in question, and posts some data to it. Iam testing with just a string. The ejb posts a string to the servlet through a BufferedWriter obtained from the URLConnection. Now, at the servlet end, i retrieve the BufferedReader from the request object (request.getReader()). I get the BufferedReader object, but when i try to read from the reader, the application stops, it basically does not proceed, neither does it throw any exception. Iam not able to figure out why ??? Could someone help me out. Find the code snippet below. EJB --- URL ur   More>>


I have an ejb communicating with a servlet.
The ejb basically opens a URLConnection to the servlet in question, and
posts some data to it. Iam testing with just a string.
The ejb posts a string to the servlet through a BufferedWriter obtained from the URLConnection.


Now, at the servlet end, i retrieve the BufferedReader from the request object (request.getReader()).
I get the BufferedReader object, but when i try to read from the reader, the application stops, it basically does not proceed,
neither does it throw any exception. Iam not able to figure out why ???


Could someone help me out.
Find the code snippet below.


EJB

---
URL url = new URL("http://localhost:8000/TestContextRoot/TestAlias");

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

huc.setRequestMethod("POST");

//huc.setUseCaches(false);

huc.setDoOutput(true);

//System.out.println("EJB: RequestProperty -> "+huc.getRequestProperty());

System.out.println("EJB: RequestMethod -> "+huc.getRequestMethod());

System.out.println("EJB: UseCaches -> "+huc.getUseCaches());

System.out.println("EJB: DoOutput -> "+huc.getDoOutput());

System.out.println("EJB: ContentType -> "+huc.getContentType());

//huc.setRequestProperty("Content-Type","text/html");

OutputStream os = huc.getOutputStream();

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

System.out.println("EJB: BufferedWriter -> "+bw);
bw.write("abc");
//bw.close();


Servlet

-------

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


String data = "";

//response.setContentType(CONTENT_TYPE);

//PrintWriter out = response.getWriter();

InputStream is = request.getInputStream();

System.out.println("Servlet: InputStream is:"+is);

BufferedReader br = new BufferedReader(new InputStreamReader(is));

System.out.println("Servlet: BufferedReader is:"+br);


while( (data = br.readLine()) != null ){


System.out.println("Servlet: "+data);


}

br.close();

//out.println("
Servlet: Hello ... Asmovic... !!");


}

   <<Less

Re: EJB communicating with a servlet using a HttpURLConnection object

Posted By:   Noel_Byron  
Posted On:   Wednesday, February 13, 2002 05:08 AM

Hi!


You call br.readLine() in your servlet. But if you do not send a newline '
' it may be possible that this call never returns (in my theory ;o)? The BufferedReader returns null if the end of the stream is reached. So if the readLine waits for a newline, the stream is never closed and the HTTP connection is not closed, too. Therefore readline will not return either will null. I'm not sure about that but I would suggest that you use a simple read method.


HTH

Noel

About | Sitemap | Contact