How to Call two servlets

Discussion in 'Java' started by RSB, Jul 25, 2003.

  1. RSB

    RSB Guest

    Hi Everyone,

    My most of programming experience is in Microsoft tools. mainly ASP. And in
    ASP we can "Include" one ASP file in an another ASP file. Can we do some
    thing similar with Servlets also..

    Say i want to Display two Lines on my Web page.

    This is First Test Line from Servlet one

    This is Second Test Line from Servlet Two.



    Where the line as it says come from servlet one and line two will come from
    servelt 2.. Or say i have a Main Servlet and now i want to call these 2
    servlets in there from my Main Servlet.

    Please help me there

    Thanks

    RSB
     
    RSB, Jul 25, 2003
    #1
    1. Advertising

  2. RSB

    Andy Flowers Guest

    RequestDispatcher dispatch =
    getServletContext().getRequestDispatcher("<path>");
    dispatch.include(request, response);

    "RSB" <> wrote in message
    news:EvbUa.870$...
    > Hi Everyone,
    >
    > My most of programming experience is in Microsoft tools. mainly ASP. And

    in
    > ASP we can "Include" one ASP file in an another ASP file. Can we do some
    > thing similar with Servlets also..
    >
    > Say i want to Display two Lines on my Web page.
    >
    > This is First Test Line from Servlet one
    >
    > This is Second Test Line from Servlet Two.
    >
    >
    >
    > Where the line as it says come from servlet one and line two will come

    from
    > servelt 2.. Or say i have a Main Servlet and now i want to call these 2
    > servlets in there from my Main Servlet.
    >
    > Please help me there
    >
    > Thanks
    >
    > RSB
    >
    >
    >
    >
    >
     
    Andy Flowers, Jul 25, 2003
    #2
    1. Advertising

  3. RSB wrote:
    > Hi Everyone,
    >
    > My most of programming experience is in Microsoft tools. mainly ASP. And in
    > ASP we can "Include" one ASP file in an another ASP file. Can we do some
    > thing similar with Servlets also..
    >
    > Say i want to Display two Lines on my Web page.
    >
    > This is First Test Line from Servlet one
    >
    > This is Second Test Line from Servlet Two.
    >
    >
    >
    > Where the line as it says come from servlet one and line two will come from
    > servelt 2.. Or say i have a Main Servlet and now i want to call these 2
    > servlets in there from my Main Servlet.


    Look up the javax.servlet.RequestDispatcher class and the
    ServletContext.getRequestDispatcher() method. You can use a
    RequestDispatcher to either include the response from one servlet in the
    response of another or to forward a request from one servlet to another.


    John Bollinger
     
    John C. Bollinger, Jul 25, 2003
    #3
  4. RSB

    Jayaram Guest

    The solution to your answer is to use JSP (JAVA Server Pages) instead.
    A JSP page is inturn compiled into a Servlet at Runtime by tour
    Servlet engine.
     
    Jayaram, Jul 25, 2003
    #4
  5. RSB

    Chris Smith Guest

    Jayaram wrote:
    > The solution to your answer is to use JSP (JAVA Server Pages) instead.
    > A JSP page is inturn compiled into a Servlet at Runtime by tour
    > Servlet engine.


    Well, the OP could do this, I suppose, but it's rather independent of
    the desired task. Including other resources (whether servlets, JSPs, or
    files) is equally possible - and about as easy - in both JSPs and
    servlets.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 26, 2003
    #5
  6. RSB

    RSB Guest

    Hi Andy,
    Thanks for the Response.. but i still need some more help .. i am going to
    copy piece of my code ..
    i have created 2 servlets..
    test1 and test2

    i also have a my own html servlet which have the htmlPageHead and sendPage
    methodes.

    sendPage methode looks like this..

    public String sendPage (HttpServletResponse response, String page)
    throws ServletException, IOException
    {
    PrintWriter out = null ;

    try
    {
    response.setContentType ("text/html") ;
    out = response.getWriter() ;
    out.println (page) ;
    out.close() ;
    } catch (Exception e)
    {
    return ("error sending HTML page to the user:\n" +
    e.getMessage()) ;
    }

    return (null) ;

    } /***



    in test1 i have the following code



    page = new StringBuffer (20480) ;

    page.append (html.htmlPageHead ("INCLUDE TWO SERVLET TEST")) ;
    page.append ("<FORM NAME=adminBPGroup method='POST') ;
    page.append ("> \n") ;

    page.append ("<TABLE border = 1 id = 'tabSec' width=100%>\n") ;
    page.append (" <tr>\n") ;
    page.append (" <td class='heading'>INCLUDE TWO SERVLET TEST Line
    one</td>\n") ;
    page.append (" </tr>\n") ;
    page.append ("</TABLE>\n") ;
    page.append ("<TABLE border = 2 id = 'tabSec' width=100%>\n") ;
    page.append (" <tr>\n") ;
    RequestDispatcher dispatch =
    getServletContext().getRequestDispatcher("/servlet/test2");
    dispatch.include(request, response);
    html.sendPage (response, page.toString());








    now the Second Servlet "test2" has the following code..


    page.append (" <td class='heading'>INCLUDE TWO SERVLET TEST Line
    TWO</td>\n") ;
    page.append (" </tr>\n") ;
    page.append ("</TABLE>\n") ;

    page.append ("</FORM>\n") ;
    page.append ("</BODY>\n") ;
    page.append ("</HTML>\n") ;









    so how do i combine these two...... as i want to see the result as



    INCLUDE TWO SERVLET TEST Line one
    INCLUDE TWO SERVLET TEST Line two


    please help me..
    thanks

    RSB




    "Andy Flowers" <> wrote in message
    news:rHcUa.1608$...
    RequestDispatcher dispatch =
    getServletContext().getRequestDispatcher("<path>");
    dispatch.include(request, response);

    "RSB" <> wrote in message
    news:EvbUa.870$...
    > Hi Everyone,
    >
    > My most of programming experience is in Microsoft tools. mainly ASP. And

    in
    > ASP we can "Include" one ASP file in an another ASP file. Can we do some
    > thing similar with Servlets also..
    >
    > Say i want to Display two Lines on my Web page.
    >
    > This is First Test Line from Servlet one
    >
    > This is Second Test Line from Servlet Two.
    >
    >
    >
    > Where the line as it says come from servlet one and line two will come

    from
    > servelt 2.. Or say i have a Main Servlet and now i want to call these 2
    > servlets in there from my Main Servlet.
    >
    > Please help me there
    >
    > Thanks
    >
    > RSB
    >
    >
    >
    >
    >
     
    RSB, Jul 28, 2003
    #6
  7. RSB

    RSB Guest

    Thank you very much Andy once again....


    "Andy Flowers" <> wrote in message
    news:pxfVa.3924$...
    Since the second servlet is just being used as a String provider it's
    probably best to include a getContent type method and call that on an
    instance of the class.

    ie.

    import <package>.Test2

    class Test1 extends ...
    {
    ...
    page = new StringBuffer (20480) ;
    ...
    page.append (" <tr>\n") ;

    Test2 test2 = new Test2();
    page.append( test2.getContent()); // just use Test2 instance
    html.sendPage (response, page.toString());
    ...
    }

    class Test2 extends ...
    {
    public String getContent()
    {
    }
    }


    That is one way. to be honest if you want to have textual includes you are
    far better adopting JSP for you presentation code and reserving servlets for
    such things as controllers.

    Take a look at the MVC pattern from the J2EE guides
    (http://java.sun.com/blueprints/index.html).

    Here you [basically] have the controller servlet (or servlets if you have a
    front controller and several supporting ones) take and parse all requests.
    This controller servlet instantiates the required model classes (JavaBeans
    or EJB) necessary for the View (JSP) to display what it needs.

    "RSB" <> wrote in message
    news:OmeVa.982$...
    > Hi Andy,
    > Thanks for the Response.. but i still need some more help .. i am going

    to
    > copy piece of my code ..
    > i have created 2 servlets..
    > test1 and test2
    >
    > i also have a my own html servlet which have the htmlPageHead and

    sendPage
    > methodes.
    >
    > sendPage methode looks like this..
    >
    > public String sendPage (HttpServletResponse response, String page)
    > throws ServletException, IOException
    > {
    > PrintWriter out = null ;
    >
    > try
    > {
    > response.setContentType ("text/html") ;
    > out = response.getWriter() ;
    > out.println (page) ;
    > out.close() ;
    > } catch (Exception e)
    > {
    > return ("error sending HTML page to the user:\n" +
    > e.getMessage()) ;
    > }
    >
    > return (null) ;
    >
    > } /***
    >
    >
    >
    > in test1 i have the following code
    >
    >
    >
    > page = new StringBuffer (20480) ;
    >
    > page.append (html.htmlPageHead ("INCLUDE TWO SERVLET TEST")) ;
    > page.append ("<FORM NAME=adminBPGroup method='POST') ;
    > page.append ("> \n") ;
    >
    > page.append ("<TABLE border = 1 id = 'tabSec' width=100%>\n") ;
    > page.append (" <tr>\n") ;
    > page.append (" <td class='heading'>INCLUDE TWO SERVLET TEST Line
    > one</td>\n") ;
    > page.append (" </tr>\n") ;
    > page.append ("</TABLE>\n") ;
    > page.append ("<TABLE border = 2 id = 'tabSec' width=100%>\n") ;
    > page.append (" <tr>\n") ;
    > RequestDispatcher dispatch =
    > getServletContext().getRequestDispatcher("/servlet/test2");
    > dispatch.include(request, response);
    > html.sendPage (response, page.toString());
    >
    >
    >
    >
    >
    >
    >
    >
    > now the Second Servlet "test2" has the following code..
    >
    >
    > page.append (" <td class='heading'>INCLUDE TWO SERVLET TEST Line
    > TWO</td>\n") ;
    > page.append (" </tr>\n") ;
    > page.append ("</TABLE>\n") ;
    >
    > page.append ("</FORM>\n") ;
    > page.append ("</BODY>\n") ;
    > page.append ("</HTML>\n") ;
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > so how do i combine these two...... as i want to see the result as
    >
    >
    >
    > INCLUDE TWO SERVLET TEST Line one
    > INCLUDE TWO SERVLET TEST Line two
    >
    >
    > please help me..
    > thanks
    >
    > RSB
    >
    >
    >
    >
    > "Andy Flowers" <> wrote in message
    > news:rHcUa.1608$...
    > RequestDispatcher dispatch =
    > getServletContext().getRequestDispatcher("<path>");
    > dispatch.include(request, response);
    >
    > "RSB" <> wrote in message
    > news:EvbUa.870$...
    > > Hi Everyone,
    > >
    > > My most of programming experience is in Microsoft tools. mainly ASP. And

    > in
    > > ASP we can "Include" one ASP file in an another ASP file. Can we do

    some
    > > thing similar with Servlets also..
    > >
    > > Say i want to Display two Lines on my Web page.
    > >
    > > This is First Test Line from Servlet one
    > >
    > > This is Second Test Line from Servlet Two.
    > >
    > >
    > >
    > > Where the line as it says come from servlet one and line two will come

    > from
    > > servelt 2.. Or say i have a Main Servlet and now i want to call these 2
    > > servlets in there from my Main Servlet.
    > >
    > > Please help me there
    > >
    > > Thanks
    > >
    > > RSB
    > >
    > >
    > >
    > >
    > >

    >
    >
    >
     
    RSB, Jul 29, 2003
    #7
    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. Peter Beck

    Threading - ASP. Net vs JSP/Servlets

    Peter Beck, Nov 19, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    721
    S. Justin Gengo
    Nov 19, 2003
  2. No One

    Servlets?

    No One, Oct 15, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,302
    John Saunders
    Oct 18, 2004
  3. John C. Bollinger

    Re: Servlets and form posts/querystring

    John C. Bollinger, Jun 27, 2003, in forum: Java
    Replies:
    4
    Views:
    6,758
    John C. Bollinger
    Jul 1, 2003
  4. GenxLogic
    Replies:
    3
    Views:
    1,291
    andrewmcdonagh
    Dec 6, 2006
  5. Jofio
    Replies:
    3
    Views:
    114
    BootNic
    Oct 9, 2005
Loading...

Share This Page