Jeff Aldrich wrote:
> A couple of questions for the group:
>
> 1) Is it possible to access the HttpServletRequest object from within a
> JavaBean in a JSP page without passing it in explicitly with a method
> such as <% bean.processRequest(request); %>?
>
No. Servlets (and the servlets generated by JSP pages) are just Java classes, so
they follow all the standard rules about referencing variables.
You might be tempted to try doing this with static variables, but that won't work
either -- you are running in a multi-threaded server, so there are quite often
multiple requests active simultaneously. Using a shared variable would cause your
app to fail.
>
> 2) What is the best/cleanest way to gain access to the real path to a
> resource from within a JavaBean in a JSP page? I am familiar with
> the ServletContext.getRealPath(virtualPath) method, but, as with
> #1 above, would rather not have to get the real path within the JSP
> page and pass it into the bean if there is a way to do it without
> using a scriptlet. My ultimate goal here is to read a list of image
> files from a local directory and display them randomly for each
> request.
>
You should use ServletContext.getResource() instead. It takes a partial path and
returns a URL for it. To read the resource as well, use
ServletContext.getResourceAsStream().
One subtle advantage to this approach -- it will work even in advanced servlet
containers where your resources are not actually stored in disk files on your web
server (and therefore do not have real paths). For example, you can configure
Jakarta's Tomcat server to load resources from an external web server, and
includes partial support for loading directly from a JAR file -- in neither case
would getRealPath() be useful.
>
> Thanks in advance,
> --jeff
>
Craig McClanahan
===========================================================================
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