• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

send response to calling servlet

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 7254
1395
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using HttpServletResponse.sendRedirect(destination);
 
Devaka Cooray
Marshal
Posts: 7254
1395
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure - using new URL(..).openConnection() etc. you can access any local or remote HTTP resource.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. thank you..
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic