Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shreeya Joshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use following methods.

urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setAllowUserInteraction(true);
 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic