• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Call a servlet from another servlet

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that meas can't we call a servlet from another servlet?
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 27
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyway why we don't transfer control from servlet to another.
Is it not a best practise?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.Thanks bear.
Your answer is much enough
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic