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
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.
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.
Joined: Jan 08, 2010
anyway why we don't transfer control from servlet to another.
Is it not a best practise?