This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Servlets and the fly likes send response to calling servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "send response to calling servlet" Watch "send response to calling servlet" New topic
Author

send response to calling servlet

Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
hi friends,

from servlet1 , i am calling servlet2, doing something in servlet2.
is it possible to send result from servlet2 to servlet1?.

thanks in advance..
Devaka Cooray
ExamLab Creator
Sheriff

Joined: Jul 29, 2008
Posts: 3698
    
  87

Selva Prakash wrote: i am calling servlet2

How do you call it?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
using HttpServletResponse.sendRedirect(destination);
Devaka Cooray
ExamLab Creator
Sheriff

Joined: Jul 29, 2008
Posts: 3698
    
  87

It's not the servlet calling the another servlet. HttpServletResponse.sendRedirect instructs the browser to do the redirection - it's the browser, which initiates a new request to the given destination. So there's no direct way for servlet1 to have an acknowledgement. Why do you want to do that?
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
ok.. this is asked by interviewer...
is any other way to call servlet from another and send back result to called servlet..?
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
Sure - using new URL(..).openConnection() etc. you can access any local or remote HTTP resource.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Selva Prakash wrote:ok.. this is asked by interviewer...
is any other way to call servlet from another and send back result to called servlet..?


Sure. Use Ulf's method or make the servlets share data across a session variable.

The correct answer of course is the mention that this is a bad idea. Makes code hard to read and understand. A filter would do nicely if you need pre-controller processing.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Reyaz Ahmed
Greenhorn

Joined: Jan 28, 2011
Posts: 3
Hi ,

You are able to called the logic or function implemented in one servlet from another servlet through RequestDispatcher object by calling the include method like
this


RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/Relative Path of another servlet");
if (dispatcher != null) dispatcher.include(request, response);








Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Reyaz Ahmed wrote:Hi ,

You are able to called the logic or function implemented in one servlet from another servlet through RequestDispatcher object by calling the include method like
this


RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/Relative Path of another servlet");
if (dispatcher != null) dispatcher.include(request, response);



Reyaz, the OP's question is about letting servlet1 know about the processing done in servlet2 and not about calling servlet2 from servlet1
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
thanks for your reply. but my question is , like function calling, i need to pass some value from servlet2 to servlet1. is it possible?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63341
    
  70

Of course not. You'll either need to use the normal HTTP request parameter mechanisms, or store the values in the session to retrieve in the next request.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Selva Prakash
Ranch Hand

Joined: Sep 03, 2009
Posts: 41
ok.. thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: send response to calling servlet
 
jQuery in Action, 3rd edition