Eric Fleming wrote:
>
> I am a CF developer that has been working with JSP recently. In Cold Fusion,
> we can set application variables in the application.cfm file that gets
> processed before each page at request time. For example, say I wanted to set
> the title of every page to "My Website", could I create a parameter in the
> web.xml file that would allow me to call it on every page and if it needed
> to be changed, just change it in one place. I can do it using session
> variables, but I would not like to do this. I was looking into the web.xml
> file and thought that I may be able to do something similar in JSP using the
> web.xml file, but I have not been able to do so. If anybody can give me some
> advice on doing this, it would be greatly appreciated.

For simple strings, you can specify them as context init parameters in the
web.xml file:

<web-app>

  <description>
    My web app
  </description>

  <context-param>
    <param-name>myTitle</param-name>
    <param-value>My Website</param-value>
  </context-param>

In your JSP pages, you can access the value like this:

  <%= application.getInitParameter("myTitle") %>

or, better, write a simple custom tag that gets the parameter value:

  <foo:appParam name="myTitle" />

If you need access to more complex data structures than strings, you
can implement an application life cycle event listener that creates
the objects when the application starts and saves them as context
attributes. They are then available as application scope objects in
all JSP pages. Listeners are new in Servlet 2.3/JSP 1.2, but for
JSP 1.1 you can do the same thing with a servlet loaded at application
startup. For more about listeners, see my JSP 1.2 article:

  <http://www.onjava.com/pub/a/onjava/2001/10/10/jsp.html?page=1>

For more about using a servlet instead, see my JSP 1.1 book:

  <http://TheJSPBook.com/>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to