• 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

Since ServletContext's getServlet(...) is deprecated, how do i get a servlet?

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do we get Servlets or call them/their methods from another Servlet? I can't use <ServletContext>.getServlet() since it's deprecated (along with all related methods) and I refuse to use a deprecated method.
Anyone know?
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest I don't know the answer. But am just curious why would you want to call any other method on servlet than get/post thro.' forward or include.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't want to have to forward to a url every time I want to use a servlet that does processing. In asp there was a way that you could execute another asp page (like a servlet), passing it the request/response objects, but never leave the page you're on. That means you don't have to make a trip out to the browser. If I have a servlet that does some good stuff and can be hit either by URL or by just processing, I could do:
//IN MY JSP PAGE
getServlet( "DBServlet" ).doPost( ... );
//NEVER LEFT SO CAN FINISH
//NO ERROR THROWN, EVERYTHING OK...
out.println( blah, blah...);
Also, I can have a DB servlet that has DB sepcific methods I can use as well as the doPost/get methods. I see tons of uses for this.
 
Author
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Paris:
If I have a servlet that does some good stuff and can be hit either by URL or by just processing, I could do:
//IN MY JSP PAGE
getServlet( "DBServlet" ).doPost( ... );


In that case, all you need to do is replace getServlet( "DBServlet" ).doPost( ... ); with request.getRequestDispatcher( "/DBServlet" ).include(request,response) ;.
You can also specify any parameters to DBServlet via query string as request.getRequestDispatcher( "/DBServlet?param1=value1" ).include(request,response);
However, note that you cannot explictily specify POST or GET methods to the included servlet in this way. The included servlet 'sees' the same method, POST or GET, that was sent to the original servlet. To send a POST request specifically, you can extend HttpSerlvetRequestWrapper, implement HttpSerlvetRequestWrapper.getMethod() and pass an instance of that subclass to the include() method. I am not sure about the exact details of what can go wrong if you change the method from GET to POST as I haven't come across such a requirement to try it out myself.
-j
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in addition, request.getRequestDispatcher(...).forward(request, response) is also handled on the server-side and would not require a trip to the browser. It is unlike that of the response.sendRediredt(...) method where a redirect message has to be sent to the browser that then executes it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic