File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes URLConnection for Cross Context Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "URLConnection for Cross Context Communication" Watch "URLConnection for Cross Context Communication" New topic
Author

URLConnection for Cross Context Communication

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi All,

I'm looking for a common topic "Cross Context Communication" which is mentioned as Container dependent in many posts. Before I can ask my question, a little back ground on my problem.

We have many applications which needs one common enterprise service to handle one common object. All these applications are capable enough to post Http requests, though designed using various technologies like VB, ASP , Java etc. Because of various SLA's in place, it is determined to design this as every application would post their request using Http to this enterprise service which is basically having a servlet to accept the rquest. Once the service is done, the servlet would post back its results to each of the calling applications again using Http. To put it simple, calling App A uses Http to App B. App B uses Http to send back the results to App A. These applications and service may or may not co-reside in one app server. Mostly, may not reside together.

There are two tasks in front of me. How can I really make this communication possible? How can I do an integration testing ? Regarding Integration testing, I have already posted in the required forum, but any ideas from this forum is also welcome.

Since Cross Context communication is container dependent, how feasible it is to use URLConnection to post the http request. I tried to do this but did not work for me. Please find the code snippet below, and please let me know If I'm missing any important link here.

<blockquote>code:
<pre name="code" class="core">
ControllerOne.java // This is one servlet in app A

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
URL url = new URL ("http://localhost:7001/WebTwo/ControllerTwo.do");
StringBuffer sb=new StringBuffer();;
URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintWriter out = new PrintWriter(con.getOutputStream());
java.util.Enumeration e=request.getParameterNames();
while(e.hasMoreElements()){
sb.append(request.getParameter((String)e.nextElement()));
sb.append("&");
}
out.print(sb.toString());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
</pre>
</blockquote>

ControllerTwo.java of App B
<blockquote>code:
<pre name="code" class="core">
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(getServletContext());
byte[] b=new byte[2048];
System.out.println("In Post");
ServletInputStream in= request.getInputStream();
System.out.println(in.available());
while(in.read(b)>0){
System.out.print(b.toString());
}
}
}
</pre>
</blockquote>

I had these two applications up and running in weblogic 10.0, and found that all the request is not reaching to WebTwo at all.

System.out.println("In Post"); is never displayed. Can some one throw me an option here.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
I don't mean to be pushing you harder, but I'm in real urgency to get this deeper into my brain. I apologize for this. But can some one throw some light, why my code failed in using URLConnection. I tried even HttpURLConnection but no use. Any pointers to an example is highly appreciated.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
I have a very slight variation of the code posted earlier and trying to put things simpler..

I have a Servlet-A running in its own web application on container - A, I have another servlet - B running in its own web application on container - B.

doPost methods of both the servlets are given below

Servlet - A
<blockquote>code:
<pre name="code" class="core">
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
System.out.println("I'm in Post of Serv-1");
// urlString="http://localhost:7001/ServletTwo/servlettwo"
URL url = new URL(urlString);
StringBuffer sb=new StringBuffer();
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setUseCaches(false);
con.setRequestProperty("Content-type", "application/octect-stream");
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
System.out.println("Request Method is:"+con.getRequestMethod());
ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());
Map m=new HashMap();

m.put("userName",request.getParameter("userName"));
m.put("pwd",request.getParameter("pwd"));
oos.writeObject(m); //Sending one parameter to second servlet

oos.flush();
oos.close();
oos = null;
} catch (Exception e) {
e.printStackTrace();
}
}
</pre>
</blockquote>

doPost of Servlet - 2
<blockquote>code:
<pre name="code" class="core">
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
System.out.println("Hi");
ObjectInputStream ois=new ObjectInputStream(request.getInputStream());
Map m=(HashMap)ois.readObject();
String userName=(String)m.get("userName");
String pwd=(String)m.get("pwd");

// Some logic to process the inputs.
//But for now we return success
String resp="Success";
// So much of other process is skipped here.
}
catch(Exception e){
e.printStackTrace();
}
}
</pre>
</blockquote>

I have both these servlets up and running in its own container and when I invoke Servlet One and post the request, I'm expecting to have "Hi" displayed from Servlet Two, but it is not happening. Can some one explain me "Why". If I can get this worked for me, then I can try to use the same technic to post back to Servlet - A .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLConnection for Cross Context Communication
 
Similar Threads
regular expression for not matching
EJB QL & heterogeneous relationships
JBoss EAR structure
StringBuffer / StringBuilder methods
Illegal Escape Character