File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem in reading from an InputStream when sending data across two applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in reading from an InputStream when sending data across two applications" Watch "problem in reading from an InputStream when sending data across two applications" New topic
Author

problem in reading from an InputStream when sending data across two applications

Pramod Chris
Greenhorn

Joined: Oct 15, 2004
Posts: 21
Hi,
I am supposed to send a String data from a JSP in one application to a servlet in another application (they reside on different servers). Now in the JSP,I have used the URL of the servlet to open a HttpURLConnection and I am writing the String into a DataOutputStream. I have done a setRequestMethod("POST") to post my data to the servlet. But I'm unable to read any data from the request object in the servlet. Please help me,thanks in advance,
Regards,
pramod.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

When is the data set? The JSP is executed when the user makes a request for the JSP's URL. If the user is supposed to fill in a form and submit the data, it isn't there to be sent. In this case your servlet which processes the POST of the form should forward the data to the second app.
Perhaps you need to give us a little more detail as to the sequence of events in your problem.
In any case, this is not an IO question. I'm going to move this topic to the Servlets forum. Maybe those knowledgable folks will see something I missed.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Shreeya Joshi
Greenhorn

Joined: Nov 29, 2004
Posts: 6
Use following methods.

urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setAllowUserInteraction(true);
Pramod Chris
Greenhorn

Joined: Oct 15, 2004
Posts: 21
Hi,
Thanks a lot for the replies. I actually worked it out last night and I managed to get over the problem. But I'll tell you what the problem was and how I went about it. And if there is a better way, maybe I can know about it too.
Say I have two applications, App1 and App2, residing on 2 different servers. Now App1 needs to perform certain operations which are actually done in App2. So a JSP in App1 has to send a request in a known format (actually an XML, but it is converted to a String object by App1 before it starts communicating with App2).This request will be read by a servlet in App2. Depending on this request, it has to send a response back (which will be again an XML converted to a String object).
So what I have done is, in App1,using the URL of the App2 servlet (which has to handle the request) I have opened a URLConnection like this:

url = new URL("http://localhost:8100/uc20/servlet/SchedulerServlet");
uc = (HttpURLConnection)url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestMethod("POST");
uc.setRequestProperty("Content-type", "html/text-plain");
uc.connect();
and I wrote the String to an OutPutStream using DataOutputStream and closed it.

On the other side the servlet in App2 looks like this: - (here I'm simply sending the same request back for testing purposes)

public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{

String response= "";
InputStream iStream = req.getInputStream();
try{
InputStreamReader in = new InputStreamReader(iStream);
int chr = in.read();
while (chr != -1) {
response += String.valueOf((char)chr);
chr = in.read();
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("SchedulerServlet::inputString-->" + response);
PrintWriter out = res.getWriter();
out.write(response);
}

public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{

doGet(req,res);
}


Earlier I was reading into a byte array and somehow wasn't able to get my data. Seems like it works now. Sorry for the trouble.
Thanks again,
Pramod.
 
 
subject: problem in reading from an InputStream when sending data across two applications
 
Similar Threads
Servlet to Applet
Doing a POST and req.getParameter - No data ?
Help needed in preventing data being inserted twice.
updating a table using Ajax?
how to upload a file using swing to web server