(Andrew Purser) wrote in message news:<. com>...
> As you can tell by my subject line I am fairly new to java and
> servlets.
>
> I was hoping I could get some feed back as to the merits / problems /
> suggestions of other ways to do it for the code skeleton below.
>
> Basically I have created a Something java class that I want to call
> from a servlet.
It is common to call other classes from servlets. That is one of the
most powerful things in servlets, you have full access to everything in
Java.
> The catch is I want the Something java class to
> include Jsps depending on various things. The only way I can see to do
> this if for my servlet to pass the ServletContext, HttpRequest and
> HttpResponse to the class. I get the feeling this may be a bad thing
> to do. Can someone enlighten me?
Instead of passing the ServletContext you can just get it via 'request':-
RequestDispatcher rd = request.getRequestDispatcher ("blah.jsp");
rd.include(...);
Servlets/YourClasses/JSPs/Filters/Taglibs can inter-operate quite well
but under some conditions you can end up making a recursive call.
The thing you include is included again and again. Cute to see
but not useful
A more common approach is to *set* something in a scope variable
from say your servlet (or classes it calls). Like this:-
synchronized (session) { session.setAttribute("blah", myBlahObject); }
Then from some other place you would simply getAttribute("blah")
and do with it as you wish.
Cheers.
Alex Kay.
>
> Cheers,
>
> Andrew.
>
>
>
> Code outline :
>
> public class Something {
>
> public void buildPage(ServletContext context,
> HttpServletRequest req,
> HttpServletResponse res) {
> // do various bits and pieces to work out what page
> // to include.
>
> context.getRequestDispatcher(jspToInlucde).include (req,res);
>
> }
>
> }
>
> public class MyServlet extends HttpServlet {
>
> doPost() {
> Something.buildPage(context,request,response);
> }
> }