• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bundle, in one config file, web parameter names?

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume a typical Java web application, with a data entry jsp, a query jsp, and an output jsp, together with some kind of query Bean that transfers parameter values between these jsp's and the business objects. Let's assume that the list of parameters passed to and from these pages and objects remains the same. A book ordering catalogue page might, for example, use parameters such as author, title, publisher, year of publication, etc.

Is there any way I could bundle these parameter names in a single configuration file so they could be loaded at server startup time by all of these pages and by the query bean? Otherwise, as we all know, add one parameter and we can look forward to making changes to each and every page, and to the query bean...something I don't want to do.

I suppose I could use an init-param, but somehow that does not seem best; it would be better to bundle these business-level params together and differentiate them from server intialization parameters, etc. Servlet params are on the other hand too narrow. I would want all the pages and the bean to have access to them.

I suppose some kind of singleton context bean might work...but I bet there's a handier solution.

Any ideas?
[ October 07, 2006: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I suppose I could use an init-param, but somehow that does not seem best; it would be better to bundle these business-level params together and differentiate them from server intialization parameters, etc. Servlet params are on the other hand too narrow. I would want all the pages and the bean to have access to them.



<context-param> ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things...

Firstly, if you are using a JSP to perform processing such as queries, it's not a "typical" application. It might have been 5 or 6 years ago, but not now. Using a JSP for anything other than display has seriously fallen out of favor.

But that's rather irrelevant to your question. While it is a good idea to centralize things like request param names, I don't see how you intend to do so via init or context params in any sort of clean manner. That's not the intent of those mechanisms and they are ill-suited to such a use.

One popular means to accomplish this is to use string constants. The constant is defined once, and then used throughout the remainder of the program. Any change to a param string propogates throughout the application via a recompile.

Do not fall into the trap of creating a single class to export all such constants for the application -- that's a serious blow to reusability and encapsulation. Rather, defined the constants where they make sense. perhaps in the servlet controllers that will deal with them, or, if you are creating form abstractions as I tend to, that's a good place to make the definitions.
 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks--Sven, context-params might indeed work. Bear, "form abstractions" sound like just what I am looking for. What's an example of a form abstraction?

And yes, I agree with you--by "query jsp" I simply meant a query form. So a way of abstracting forms--or defining form labels and default values using string constants in some kind of form abstraction would be very useful, I would think.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For form abstraction, how about XForms as worked up by the W3C.
That may be too complex for your application but worth a look.

Here is an open source implementation at Sourceforge.
Here is a survey article on XForms.

Bill
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just learned about something called a "ServletContextListener", but apparently Bear is putting the kibosh on that. The container will call a ServletContextListener when your application begins, so you can read the context initialization parameters and then create an object and populate it with the context initialization parameter values. Then, you can attach your data object to the ServletContext object, which is available to any servlet in your app.

Bear, I'm wondering if you could comment on when you would use a ServletContextListener. My book(HF Servlets & JSP) gives the example of a database connection. Are there others?
[ October 08, 2006: Message edited by: sven studde ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For exactly the type of initialization you are talking about -- anything that needs to get loaded up at web app startup.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In this thread:

https://coderanch.com/t/363442/Servlets/java/where-should-create-database-connection

they are saying that using a ServletContextListener to open a database connection is a bad idea. So, my only example of when to use a ServletContextListener has been shot down.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The use of context listeners is a bit of a branch from the original topic, so I'm going to ask that if you'd like to discuss some of the things that context listeners are being used for, please start a new topic on that subject.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it, the op's question was how to bundle data together and make it available app wide. The op also metioned initialization parameters. Since you can use context initialization parameters in conjunction with a ContextServletListener to bundle data into an object and make it available app wide, I don't see how exploring whether that is a good option or not is off topic.
[ October 09, 2006: Message edited by: sven studde ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it, the op's question regards how to centralize the names used for request parameters. Even if it were determined that a context listener was a good mechanism for that, a general discussion of "what usages can a context listerner be put to' would be enough of a branch to warrant a new topic.
[ October 09, 2006: Message edited by: Bear Bibeault ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic