• Post Reply Bookmark Topic Watch Topic
  • New Topic

A better approach to declaring variables in JSPs

 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a better way to declare vars in jsps I have about 12 jsp pages where the below code is repeated for variables passed in to my taglibs.

If I want to add a variable I have to do it in 12 places.

Reg


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice is to punt back and remove all Java from your JSPs and adopt scriptless pages using an MVC architeture and the JSTL/EL.

But if that's too big a step for you I'd at least put the common code into an include file so that it exists only once in the source tree.

But think of my first suggestion. Seriously.

Really.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Thanks for the reply.

If I use a taglib let say the one below I'm passing these parameters
by appending to a url, I still have to declare the vars in my jsp page for it to work and use request object to get the parameters.

Is there another way to declare and get the params, or do I still have to
have some scripting in my jsp?

Reg



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to say without more context.

Are you running JSP 1.2 or JSP 2.0? It's much easier with the latter to simplify your JSP pages.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Weblogic 8.1 which support the 1.2 spec

Reg
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you are kind of stuck using scriptlet expressions for the dynamic attributes of custom tags. Under JSP 2.0, you can use the EL for these.

In prep for that, I would look into factoring out as much of the Java as possible from the pages, but you won't be able to do so 100%.

How are all these variables getting set? Why do you need to carry them page to page? If they are a common set that are used on multiple pages, why are they not session variables?
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I should set these in the session object? and get them as I need them?

Reg
 
Dan Vos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Reggie McDougal:
So I should set these in the session object? and get them as I need them?

Reg


Does using value bean and passing them via either session or request make it easier for you?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they are indeed values that need a scope higher than the page/request level, then the session is the appropriate place for them if they are per user. If they are application global, then the application context can be used.

In either case, be sure to clean up after yourself when you are done with them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!