How to Call two servlets

R

RSB

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
 
A

Andy Flowers

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

John C. Bollinger

RSB said:
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
(e-mail address removed)
 
J

Jayaram

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.
 
C

Chris Smith

Jayaram said:
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
 
R

RSB

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




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

RSB

Thank you very much Andy once again....


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top