Hi list! Long time lurker, first time poster. I've been trying to
figure out how to do this all day and it's driving me crazy. Any help
greatfully accepted! Also, apologies for this being slightly OT --
it's more about J2EE in general than Tomcat. If anyone knows a decent
J2EE mailing list they could point me to I'd be very happy, but I
couldn't find any after googling for a while, and you folks all seem
to know so /much/.

I have a webapp that has a single JSP page. The JSP page scrapes data
from two remote HTML pages and presents it. This scraping needs to
only happen on demand (i.e. when the page is loaded) and at most every
NNN seconds (it's a monitoring app and I don't want it to turn into an
accidental DoS!)

The scraping itself is done by one Bean object for each remote source.
These looks a bit like:

(Please forgive errors in my code snippets, I'm rewriting them in
Gmail's compose box so there might be small syntax problems)

===============================
abstract class Scraper() {
  public abstract void doScrape();

  protected URL url;
  public void setUrl(String url) {
    this.url = new URL(url);
  }
 
  protected scrapeIfNecessary() {
    // check a timeout; if we haven't scraped in the last
    // NNN seconds, then:
    doScrape();
  }
}

class ScraperOne() {
   // ideally I'd like a one-arg constructor with the URL in, but I have to 
   // make this class a bean for the JSP page, so no-args it must be
  public ScraperOne() {
    super();
  }

  private String dataOne, dataTwo;
  public void doScrape() { 
    // loads from this.url
    // puts results in dataOne, dataTwo
  }
  public void getDataOne() { scrapeIfNecessary(); return this.dataOne;}
  public void getDataTwo() { scrapeIfNecessary(); return this.dataTwo;}
}
// ScraperTwo is similar
===============================

Now, up until now, I had lines in the JSP that looked like this:

===============================
<jsp:useBean id="data1" scope="application" class="foo.ScraperOne">
  <jsp:setProperty id="data1" name="url" value="http://www.foo.com/foo.html"/>
</jsp:useBean>

<p>I got data ${data1.dataOne}.
===============================

Now, however, I want to move that URL configuration out of the JSP and
into a config file. Thing is, I'm not really sure on how I'm supposed
to make a webapp read a configuration file on startup then make that
config available to all objects that need it. In the past I've done
this with a load-on-startup servlet and a Singleton to store the
config, but I know that's a bit of a kludge.

After some research and messing about, I've created an implementation
of ServletContextListener that can read an XML file, create an
instance of my ConfigurationBean class, then put it in the context
with:
  ServletContext.setAttribute("config", myConfigurationBean)

My first problem with this is when I use it in my JSP:
  <jsp:useBean id="config" scope="application" class="foo.ConfigurationBean"/>
I get this error:

===========================
  org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 10 in the jsp file: /index.jsp
Generated servlet error:
/usr/local/jakarta-tomcat-5.0.29/work/Catalina/localhost/foo/index_jsp.java:51:
config is already defined in
_jspService(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
      foo.ConfigurationBean config = null;
===========================

It looks like the jsp:useBean tag is trying to declare another
application-scoped ConfigurationBean. I would have expected it to read
in the one declared in the ServletContextListener instead. What have I
misunderstood?

My second problem: assuming that I get the ConfigurationBean correctly
set up in the JSP, what is the best way to pass the appropriate values
through from it into the DataOne scraper? Is it just with a Java
snippet in the JSP, or am I missing a trick? Something like this:
===========================
<jsp:useBean id="data1" scope="application" class="foo.ScraperOne">
<% data1.setUrl(config.getDataOneURL()); %>
<!-- or maybe -->
<% data1.setConfig(config); %>
===========================

With JSP2 though I thought we would be avoiding Java in JSPs like
this... is there a simple way to pass arbitrary values out of an
application-level bean and into arbitrary Java classes?

Sorry for this rather exhaustingly long post. Again, any suggestions
will be very greatfully received. I'm sure this whole configuring
webapps thing must be well discussed somewhere but I've been googling,
searching JavaWorld, developer.ibm.com, sun.com etc without ever
finding a clear, easy "do this, then this" type guide.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to