Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using application wide variables on jsp's

 
Ryan Anklam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a context listener put the "constant" into application scope where it can be accessed by any page.
 
Ryan Anklam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a class that implements ServletContextListener, and then declare it in the deployment descriptor.
 
Ryan Anklam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic