aspose file tools*
The moose likes JSP and the fly likes Using application wide variables on jsp's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using application wide variables on jsp Watch "Using application wide variables on jsp New topic
Author

Using application wide variables on jsp's

Ryan Anklam
Greenhorn

Joined: Apr 12, 2011
Posts: 7
An application I'm working on uses the displaytag library and I'd like to set an application wide variable for the pagesize attribute:



I'd like to replace the hard coded page size with something I can set once and read from all the pages. I was thinking as a key in my struts applicationresources.properties file but I don't know how to access that value outsize of the <bean:message> tag.

Most of my background is from ColdFusion where I have an onApplicationStart method where I can store application level properties when my application is started. I can access these properties in my dynamic pages by prepending the variable name with application. . For instance in my onApplicationStart method I'd set:

[code=java
application.pagesize = 50;
[/code]

Then in my .cfm pages (similar to my .jsp's) I could use it like:



I'd like to do something similar in my jsp's but I'm not sure how to do it. Anybody have any suggestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

In a context listener put the "constant" into application scope where it can be accessed by any page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ryan Anklam
Greenhorn

Joined: Apr 12, 2011
Posts: 7
Thanks for the response, I appreciate it. I tried searching around for what I'm looking for but I don't think I was using the right search terms to find what I was looking for.

I'm pretty green in the Java world, migrating from mostly PHP and ColdFusion. Would you be able to explain a little more about adding a context listener? Is there a method that gets invoked when the application is started that I could use?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

That's exactly what a context listener is. A method in the listener is invoked at application startup to perform one-time setup tasks such as this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Create a class that implements ServletContextListener, and then declare it in the deployment descriptor.
Ryan Anklam
Greenhorn

Joined: Apr 12, 2011
Posts: 7
I'll give this a shot. I'm pretty sure I understand what you are getting at here. Thanks for the help!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

Once you've looked this up (use the javadoc and the Servlet spec) post back with any problems you have. This is an important skill to have under your belt if you will be working in web apps.
Ryan Anklam
Greenhorn

Joined: Apr 12, 2011
Posts: 7
I've got the context listener working, but I ran into what is most likely a simple problem. Here is my context listener code:



The problem that I'm having is that its not loading my xml configuration file. It looks like its looking for the config file (see line 19) right in my tomcat's bin directory. How can I get it to point to my webroot directory?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

A relative file path? How is that supposed to work given that there is no deterministic meaning to "current directory" in a web app?
Ryan Anklam
Greenhorn

Joined: Apr 12, 2011
Posts: 7
So it wasn't the relative path I was looking for it was the current context path. Once I had that I could append my relative path to it to get the file I was looking for.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using application wide variables on jsp's