Passing Servlet Context to a non servlet object.

Discussion in 'Java' started by Andrew Purser, Nov 3, 2004.

  1. 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. 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?

    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);
    }
    }
    Andrew Purser, Nov 3, 2004
    #1
    1. Advertising

  2. Andrew Purser

    Sudsy Guest

    Andrew Purser wrote:
    > 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. 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?

    <snip>

    If class Something depends on servlet container facilities, why are you
    trying to break it out into a separate class? Sounds like the function-
    ality belongs in the servlet. Your architecture is confusing; care to
    elaborate?

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
    Sudsy, Nov 3, 2004
    #2
    1. Advertising

  3. Andrew Purser

    Chris Smith Guest

    Andrew Purser wrote:
    > Basically I have created a Something java class that I want to call
    > from a servlet. 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?


    There's nothing inherently wrong with passing the ServletContext and the
    ServletRequest and ServletResponse outside of the servlet class. There
    are some concerns in some cases, though, specifically with code re-use.
    If your goal is to include JSPs, then it seems you probably don't intend
    to use this outside of a servlet environment, so you're probably okay.
    If you needed something more reusable, you'd need to consider other
    options.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Nov 3, 2004
    #3
  4. Andrew Purser

    Alex Kay Guest

    (Andrew Purser) wrote in message news:<>...

    > 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);
    > }
    > }
    Alex Kay, Nov 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,790
    jenny
    May 25, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  3. Replies:
    3
    Views:
    29,056
    Stuart Wood
    Jun 6, 2006
  4. Replies:
    4
    Views:
    678
    santax
    Apr 20, 2007
  5. -Lost
    Replies:
    7
    Views:
    172
    -Lost
    Mar 24, 2007
Loading...

Share This Page