Article View: borland.public.jbuilder.java.api
Article #2435Re: deprecated method
From: Paul Furbacher [TeamB]
Date: Wed, 29 Nov 2000 00:00
Date: Wed, 29 Nov 2000 00:00
71 lines
3206 bytes
3206 bytes
Alan,
This question belongs more properly in the "servlets-jsp" newsgroup.
But I'll answer it here. (In the future, try harder to post to
the appropriate newsgroup. Thanks.)
> I'm trying to learn about servlets from a book entitled Core Java
> Web Server and as I have been following it along with its
> various code samples I've come accross the code for a servlet which
> has a deprecated method call that I don't know how to fix.
> The method call is from class ServletContext: getServlet(String s)
> [...]
>
> public void service(ServletRequest req, ServletResponse res)
> throws ServletException, IOException
> {
> res.setContentType("text/html");
> PrintWriter pw = res.getWriter();
// next line returns "null"
> FortuneBrat fb =
> (FortuneBrat)getServletContext().getServlet("FortuneBrat");
> ...
>
> This servlet references another servlet FortuneBrat.class which
> compiled fine. FortuneBrat is set up to simply return a string.
>
> [...]
> getServlet(java.lang.String name)
> Deprecated. As of Java Servlet API 2.1, with no direct replacement.
>
> [...]
>
> In lieu of this method, servlets can share information using the
> ServletContext class and can perform shared business
> logic by invoking methods on common non-servlet classes.
>
> This is too cr[y]ptic for me. I have no idea what it is
> telling me or how I should fix this code for the PageBrat
> servlet.
The "In lieu of..." sentence is awkwardly phrased, no doubt.
What it means is that instead of invoking a servlet to do this
work, use a regular Java class. Currently, you have functional code
in "FortuneBrat" which is being "shared" by a number of servlets
perhaps -- that's the "servlets can share information [or] business
logic..." portion of the above awkward sentence. Instead of putting
the "bp.getFortune()" logic in a servlet, place it in a separate
(non-servlet) Java class. Either instantiate "FortuneBrat" when you
need it, or make its "getFortune()" method static:
public class FortuneBrat {
public static String getFortune([parameters?]) {
String theFortune = "";
... // do some processing based on parameters, if any ...
return theFortune;
}
}
There are very few reasons why you would want to get a servlet.
These are pretty much related to administrative functions, and
not your "run-of-the-mill" request handling. Your requirements
fall into the latter category.
Paul Furbacher (TeamB)
Please, no private correspondence related to
newsgroup issues. Thanks.
Technical Details
Message-ID:
<3A255BD4.4DA72190@home.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3A255571.66E8A8B1@hfx.eastlink.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,dfa9fedc3f5943c2 X-Google-Attributes: gid1089d0,public From: "Paul Furbacher [TeamB]" <pnf@home.com> Subject: Re: deprecated method Date: 2000/11/29 Message-ID: <3A255BD4.4DA72190@home.com> X-Deja-AN: 699227279 Content-Transfer-Encoding: 7bit References: <3A255571.66E8A8B1@hfx.eastlink.ca> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pnf@home.com Newsgroups: borland.public.jbuilder.java.api