Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call a servlet from another servlet

 
Khirod Patra
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that meas can't we call a servlet from another servlet?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4435
256
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
parampreet sethi
Greenhorn
Posts: 27
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyway why we don't transfer control from servlet to another.
Is it not a best practise?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.Thanks bear.
Your answer is much enough
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic