File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet call another context" Watch "Servlet call another context" New topic

Servlet call another context

Vanitha Anandan

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();

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));

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


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

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 :
Try not to become a man of success but rather to become a man of value.
Vanitha Anandan

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

Joined: Sep 08, 2009
Posts: 1
[Edit: thread hijack removed. Please ask your own questions in new topics.]
I agree. Here's the link:
subject: Servlet call another context
It's not a secret anymore!