File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Call a servlet from another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Call a servlet from another servlet" Watch "Call a servlet from another servlet" New topic
Author

Call a servlet from another servlet

Khirod Patra
Greenhorn

Joined: Mar 27, 2008
Posts: 23
Hi,

I need a task to do !! Please help me out .


Let two jsp file A.jsp and B.jsp with respective two servlet file AServlet.java and BServlet.java.Suppose I am in a method of
AServelt.java and need to execute another mothod of the BServlet.java and back to the A.jsp with modification.But whem I try to
execute this process It gives NullPointerException for the request and response object.

Another way I just tried to execute the same method from the model class(BModel.java) of the BServlet.java,just by creating the
objetc of BModel from the AModel.java,but no luck...Same Exception.

My Project Folw given below
----------------------------------

Project----|--A.jsp----->AServlet.java--->AModel.java
|
|
|--B.jsp------>BServlet.java---->BModel.java



Please let me know if any other related information required...



Thanks & Regards

Khirod Chandra Patra
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Khirod Patra wrote:Suppose I am in a method of AServelt.java and need to execute another mothod of the BServlet.java

Don't do that. Your servlet shouldn't have methods which are designed to be called from elsewhere. Refactor your design so that this method belongs to some bean class which can be instantiated wherever it's required.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
that meas can't we call a servlet from another servlet?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3093
    
  40

Samanthi perera wrote:that meas can't we call a servlet from another servlet?

It should not, and no one need to do that.

Khirod Patra wrote:But whem I try to execute this process It gives NullPointerException for the request and response object.

Does this mean you were trying the RequestDispatcher?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
parampreet sethi
Greenhorn

Joined: Aug 03, 2009
Posts: 27

Hey,

One way could be to call BServlet from AServlet by passing on the request and response,



This way the control will be transferred to BServlet's doGet method, there you can get the values from requests and call the method which is required to be called and then in the end call the jsp where you want the response to come as output.



The other benefit of using disp.forward method is that it will transfer the control internally and user will not be aware of the request being processed by BServlet.

I am not sure this will help you or not, but this is just another way of solving this issue.


Param
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
anyway why we don't transfer control from servlet to another.
Is it not a best practise?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

You seemingly keep asking different questions. Please be clear in what you are asking.

If you are asking about calling methods in another servlet, that's a no-no.

If you are talking about forwarding from one servlet to another via a request dispatcher, that's fine and a common occurrence.

If you are talking about redirecting from one servlet to another, that's also fine.

So which are you asking about?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
ok.Thanks bear.
Your answer is much enough
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call a servlet from another servlet