> You can get a
> RequestDispatcher by calling
> ServletContext.getRequestDispatcher(). Note
> that this is a new addition to the servlet spec (2.1 I
> think), and is not
> supported by all JSP implementations.
In older versions it's called getResourceDispatcher() I believe.
> Also very important to note is that the lifespan is set to
> "session". If
> set to "page", a new Bean will always be created. If set to
> "application",
> well that's okay, but the bean is goingto hang around for
> ever, and it would
> be available to every client, not just the one that made the initial
> connection.
For all three types of lifespan, the bean is only created if it doesn't
exist already.
The difference is where it is stored.
In the reference implementation it looks in the folloing places:
page: bean=(BeanType)request.getAttribute(beanName);
session: Session session=request.getSession();
bean=(BeanType)session.getValue(beanName);
application: ServletContext
servletContext=getServletConfig().getServletContext();
bean=(BeanType)servletContext.getAttribute(beanName);
None of this is in the 0.92 spec though, so although these are fairly
logical places to put the beans, there is no guarantee it will stay this
way.
If your mode 2 servlet wants to store beans that are only used in the
current page, it's even better to store it as part of the request instead of
in the session.
> -----Original Message-----
> From: Taggart Gorman [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, April 15, 1999 3:28 AM
> To: [EMAIL PROTECTED]
> Subject: Re: JSP Model 2
>
>
> Sun Lihua (NWSS/Sydney) [[EMAIL PROTECTED]] writes:
> > 1. in the spec 0.92, it introduces two models using JSP, but
> > as to the model
> > 2, there is no word in the spec or the samples on how to
> call JSP file
> > directly from Servlet or other Java classes. How could I use
> > it? Will this
> > be introduced in spec 1.0?
>
> The servlet doesn't "call" the JSP, it either redirects or
> forwards to it.
>
> One method would be to use
> HttpServletResponse.sendRedirect(String) from
> within the servlet to tell the client browser to go to the
> JSP. However,
> this causes an extra round trip from the client to the server.
> The preferred method (IMHO) in the 0.92 spec is to use
> RequestDispatcher.forward(String) to have the web server
> "automagically"
> forward input from a second URL within the same connection.
> You can get a
> RequestDispatcher by calling
> ServletContext.getRequestDispatcher(). Note
> that this is a new addition to the servlet spec (2.1 I
> think), and is not
> supported by all JSP implementations. I know for a fact that
> it is support
> in JRun 2.3. Check the JSP FAQ for more information on JSP
> implementations.
> (This API is detailed in the 0.92 spec - it's way at the end.)
>
> > 2. the purpose of JSP is to seperate the dynamic content
> > generation from the
> > presentation, so I think not only should we be able to call
> > JSP file from
> > Servlet, but also from any Java classes, is it in the
> > consideration of spec
> > 1.0?
>
> Umm, how would a random Java class "call" a JSP file?
> Since the purpose
> of a JSP file is to output HTML, it doesn't really make much
> sense to call a
> JSP. But if you really wanted to do it, your Java code could
> simple open an
> HTML connection to the web server where the JSP lives and
> request the URL of
> the JSP.
> Since the point of model 2 in JSP is to remove logic from
> the JSP, there's
> not much point in "calling" the JSP.
>
> > 3. is there a way to pass parameters to JSP file? because the
> > workhorse for
> > JSP file is servlet, which is a Java class, so I think there
> > should be a way
> > to define the variables in JSP file, and pass parameters
> > dynamically to JSP
> > file from other java classes, just as the ordinary Java class.
>
> In the servlet, create the object you want to access in the
> JSP. The
> object should be "JavaBean-like" and use get/set methods to
> access data.
> Then stuff the object into the current HttpSession, like so:
>
> MyDataBean bean = new MyDataBean ();
> HttpSession sess = req.getSession(true);
> sess.putValue("databean", MyDataBean );
>
> Then in the JSP you forward() to, use the following JSP
> tags to get the
> bean and access it:
>
> <usebean name=databean type=com.tradiant.MyDataBean
> lifespan=session>
> </usebean>
>
> <display property=databean:myData>
>
> Note that the name of the object you put into the session
> and the name in
> the <usebean> tag are the same - that's the trick to passing
> objects between
> servlets and JSPs.
> Also very important to note is that the lifespan is set to
> "session". If
> set to "page", a new Bean will always be created. If set to
> "application",
> well that's okay, but the bean is goingto hang around for
> ever, and it would
> be available to every client, not just the one that made the initial
> connection.
>
> > 4. in spec 0.92, at the top of page 13, the four implicit
> > variables are:
> > servletrequest, servletresponse, in and out.
> > but on page 29, those four implicit variables have become to:
> > request, response, in and out.
> >
> > which one is correct? (actually, "request" is an implicit
> > bean, so I think
> > page 29 should be changed according to page 13.
>
> I don't have page numbers on my speq (HTML version), so I
> can't find the
> passages in question to try and answer this.
>
> Hope this helps.
>
> ==============================================================
> =============
> 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".
>
===========================================================================
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".