aspose file tools*
The moose likes Servlets and the fly likes Servlet call another context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet call another context" Watch "Servlet call another context" New topic
Author

Servlet call another context

Vanitha Anandan
Greenhorn

Joined: Dec 06, 2007
Posts: 8
I am working on websphere with 2 application servers - one for web and one for engine.
I have a servlet running on engine which recieves the type of input and returns the string as response in PrintWriter.

In web module Inside a servlet I need to call the servlet of engine - pass the input and get the response.

I know request.sendRedirect will help me but this redirects the page to the engine.
Could you please suggest me a way by which i can pass input from my web servlet to engine servlet and get the response from engine servlet to web.

I have tried writing

url = new URL(urlStr);


HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
out.write("Type="+adoType);
out.flush();
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response;
StringBuffer sb = new StringBuffer();
while( (response = in.readLine()) != null){
System.out.println("Response " + response);
sb.append(response);
sb.append("\n");
}

in.close();

but this doesnt help me.
Please suggest.
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Firstly, assuming that you're talking about HTTPServletRequest, there's no method sendRedirect in that interface. Perhaps you meant response.sendRedirect("...")?
Now if you use that, a new request object will be created and any parameters that you received in the current request object will NOT be relayed onto the new request object created as a result of the sendRedirect.
You should probably look into RequestDispatcher.

Also, please make use of the [code] tags when you're posting code. It greatly improves readability of your posted code.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


You should probably look into RequestDispatcher.


It can't be used to forward requests to another server.

sendRedirect is the way to go
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
My bad :/ Thanks for pointing that out Balu. He's right, you can't make use of RequestDispatcher in this scenario.
In that case, what you've done - establishing a connection using HttpURLConnection seems to be a viable option. So what happens when you try using that technique? Do you face any error while using it?
Vanitha Anandan
Greenhorn

Joined: Dec 06, 2007
Posts: 8
Yes the call doesnt go the other servlet.
My assumptions :
- Is it because the url that is framed is https:??
- There should be a way using response object to establish a connection to the other servlet in the same WAS box but different application server and read the data.

Currently the call to the other servlet is not going.
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
You can try RMI... but still you cannot create a HTTP connection as you are expecting..... i really thinking is their is no option provided in j2ee to connect to a another server and get the response back???


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
Vanitha Anandan
Greenhorn

Joined: Dec 06, 2007
Posts: 8
There is one HttpClient but i need to explicitly provide username and pwd but my question is when its in the same WAS why should I use this approach. Cant i use the existing credentials for this
tamos kuk
Greenhorn

Joined: Sep 08, 2009
Posts: 1
[Edit: thread hijack removed. Please ask your own questions in new topics.]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet call another context
 
Similar Threads
Server returned HTTP response code: 405 for URL
Unable to read the Japanese character from HTTP Response
how to send a string to a servlet from a java program
FileNotFoundException Response 500
XML over HTTP