Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet to servlet  RSS feed

 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How to call from one servlet to another servlet.
Thanks
Jowsaki
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are calling one servlet from another then you probably needs to reconsider how you've coded you app. It is likely you need to extract the code used into a java bean and then you can use it in both without duplicating code or need to spin off another request reponse cycle (causing a double hit to the server for every user hit to that servlet
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sham
You may want to look at the javax.servlet.RequestDispatcher's forward() and include() methods.
Mark.
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Thanks for ur reply...
am using dispatch stuff like below.. is it correct?
request.setAttribute("msi",msi);
request.setAttribute("keyword",key);
RequestDispatcher rd=getServletContext().getNamedDispatcher("ScdsServlet");
rd.forward(request,response);
i tried this, it is not forwarding.
Can you pls advise me..
tks
cheers
jowsaki
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sham
When using getNamedDispatcher(String name), the name parameter must be the name of the servlet as defined in the web.xml deployment descriptor, for the same web application.
You can also use getRequestDispatcher(String path), where the path parameter is the path to the required resource. This method exists in both the ServletRequest and ServletContext interfaces. The ServletRequest version may be relative, whereas the ServletContext version must start with a "/".
For example:

Hope this helps
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark
Originally posted by Mark Howard:
Hi Sham
When using getNamedDispatcher(String name), the name parameter must be the name of the servlet as defined in the web.xml deployment descriptor, for the same web application.
You can also use getRequestDispatcher(String path), where the path parameter is the path to the required resource. This method exists in both the ServletRequest and ServletContext interfaces. The ServletRequest version may be relative, whereas the ServletContext version must start with a "/".
For example:

Hope this helps
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
whenever am forwarding request dispatcher.. it is return always null in my ScdsServlet.
request.setAttribute("msi",msi);
request.setAttribute("keyword",key);
RequestDispatcher rd=getServletContext().getNamedDispatcher("ScdsServlet");
rd.forward(request,response);

Can you pls advise me..
tks
cheers
jowsaki
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sham Jowsaki:
whenever am forwarding request dispatcher.. it is return always null in my ScdsServlet.

At what point exactly is a null being returned?
 
Sham Jowsaki
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark..
I solved the problems.. made mistake while passing parameters.
tks
jowsaki
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!