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
Please let me know if any other related information required...
Thanks & Regards
Khirod Chandra Patra
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 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?
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.
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?