Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

send response to calling servlet

 
Selva Prakash
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4682
331
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Selva Prakash wrote: i am calling servlet2

How do you call it?
 
Selva Prakash
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using HttpServletResponse.sendRedirect(destination);
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4682
331
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure - using new URL(..).openConnection() etc. you can access any local or remote HTTP resource.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Reyaz Ahmed
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65221
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Selva Prakash
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. thank you..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic