Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet calling another servlet...

 
Jason Kim
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Can a servlet load another servlet in different J2EE application ?
If so, could you show me how ?
Code example would be much appreicated.
Thanks.
 
Ashik Sherfudeen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think v can use Request Despatcher Interface for this. Use forward method to load the server which is in another j2ee application.
If this answer is wrong could u explain why it is wrong.
 
Amol Desai
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You cannot use the forward method of RequestDispatcher interface.
A forward method allows you to redirect only to a resource in the same Web application.
The sendRedirect method, on the other hand, allows you to redirect to any URL.
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Servlets forum...
 
Jason Kim
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet in different applications within same virtual host can call each other.
ServletContext context = getServletContext('context-root-name');
context.getRequestDispatcher('uri-of-servlet-to-be-called').forward(request,response);
If applications are in different host, one has to use URL connection or sendRedirect ?
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think using a sendRedirect() would be a solution.
forward() may have problems because redirecting to the different resource takes place on the server side in this case where as in the case of sendRedirect() a browser is made to generate a new request to the resource (which could be anything - maybe on a different server).
Pl. correct me if i am wrong or have missed anything.
-Kaustubh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic