Patrick Deloulay wrote:
> in the more recent servlet specs (2.2?), the use of
>
> public Servlet ServletContext.getServlet(String servletName) has been
> deprecated.
>
> I am right now heavily using this API to pass objects around betwen
> servlets.. I am learning/moving to the JSP API and I was wondering how I
> could perform the same kind of operations...
>
> Any ideas... Thanks for your always helpful input.
>
Since JSPs are based on servlets, you're going to run into the same
restrictions there. There is no longer any legal mechanism to get a reference
to an instance of another servlet (for valid security-related reasons), so you
can no longer use instance variables or public methods in servlets to share
things.
I share things in three different ways based on the "lifetime" and
"visibility" of the objects that is needed. In all cases, such objects are
equally visible to servlets and JSP pages, so I illustrate the syntax to store
an object with a servlet, and access it as a bean in a JSP page.
* Lifetime is just this single request, visible only to the servlets
and JSP pages that need to process this request (typically you
would be using RequestDispatcher.include or RequestDispatcher.forward
for servlets, or <jsp:include> or <jsp:forward> in JSP pages):
Servlet:
MyBean myBean = new MyBean(...);
request.setAttribute("myBean", myBean);
JSP Page:
<jsp:useBean id="myBean" scope="request" class="MyBean" />
* Lifetime is more than one request for the same user, but not
a globally shared variable:
Servlet:
MyBean myBean = new MyBean(...);
HttpSession session = request.getSession();
session.putValue("myBean", myBean);
JSP Page:
<jsp:useBean id="myBean" scope="session" class="MyBean" />
* LIfetime is the entire application, and the variable needs to be
shared across multiple users (like a database connection pool):
Servlet:
MyBean myBean = new MyBean(...);
getServletContext().setAttribute("myBean", myBean);
JSP Page:
<jsp:useBean id="myBean" scope="application" class="MyBean" />
> Patrick
>
Craig
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html