Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet call another context

 
Vanitha Anandan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Vanitha Anandan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: thread hijack removed. Please ask your own questions in new topics.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic