Someone on this list asked how to invoke JSP from
a servlet.  The following answer is from the
JSP faq at:
http://www.esperanto.org.nz/jsp/jspfaq.html
*****************************

 10) How do you invoke a JSP page from a servlet?

                   TOC


     (Contributed by:
[EMAIL PROTECTED])

     After scanning through archives of the JSP
mailing list to no effect I finally remembered that
I'd pasted this example into a
     document I wrote. It was originally sent by
Satish Dharmaraj of Sun to show the model 2 approach
(as described in the
     0.92 specification): how to pass data from a
servlet
     to a JSP.

     Create a directory called model1/ under the
samples/ directory. Place foo.jsp and Foo.java inside
this directory.

     Compile FooServlet.java and place
FooServlet.class in TOP/servlets/directory.

     Then invoke using
http://host:8080/servlet/FooServlet

     In this example, FooServlet creates a list and
then stores the result in Foo.class. Foo.Class is then
passed as a datasource
     to foo.jsp.

     The sources are:

     1) FooServlet.java

     import java.io.*;
     import javax.servlet.*;
     import javax.servlet.http.*;
     import model1.Foo;

     public class FooServlet extends HttpServlet
     {

          public void service(HttpServletRequest req,
HttpServletResponse res)
          throws ServletException, IOException
          {
           String s[] = new String[] {"blue", "green",
"red"};
           Foo f = new Foo(s);
           req.setAttribute("foo", f);


getServletContext().getRequestDispatcher("/samples/model1/foo.jsp").forward
           (req, res);
          }

     }

     2) foo.jsp

     <html>
     <usebean name=foo type=model1.Foo lifespan=page>
     </usebean>
     <ul>
     <loop property=foo:list propertyelement=x>
     <li> <display property=x>
     </loop>
     </ul>
     </html>

     3) Foo.java

     package model1;

     public class Foo {

          String s[];
          public String[] getList() { return s; }
          public Foo(String s[]) { this.s = s; }
          }

     (from "O´Hare, Thomas Bernhard"
<[EMAIL PROTECTED]>)



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to