aspose file tools*
The moose likes Servlets and the fly likes bundle, in one config file, web parameter names? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "bundle, in one config file, web parameter names?" Watch "bundle, in one config file, web parameter names?" New topic
Author

bundle, in one config file, web parameter names?

Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
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 ]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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> ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Benjamin Weaver
Ranch Hand

Joined: Apr 08, 2003
Posts: 161
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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


Java Resources at www.wbrogden.com
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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

Joined: Sep 26, 2006
Posts: 148
Hi,

In this thread:

http://www.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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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

Joined: Sep 26, 2006
Posts: 148
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bundle, in one config file, web parameter names?
 
Similar Threads
php form arrays in java
Best Strategies of Passing output data to web tier
JSP include
JSTL, indirection of attributes
Creating a class within JSP to be shared among pages