• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Creating a class within JSP to be shared among pages  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, JSPs make use of bean classes (via jsp:useBean) compiled separately and stored in a JAR/WAR or in the WEB-INF/classes folder.

However, we have a need for customers to create JSP pages in which they'd like to define their own simple beans. Today, we do that by defining a class within the JSP using the constructs. This has worked well.

However, I need to be able to pass that bean to another JSP (such as via the session or request objects), but of course that class cannot be accessed outside of the JSP/servlet created since the class is defined within that class.

Does anybody have any ideas about how such a class could be created and/or passed to other pages? Is there a better way to define a class within a JSP that can be shared with other JSPs? In this case, one JSP got so big it needed to be split into two pages, but the bean needs to be shared by those two pages since it has a lot of data from request parameters, the database, etc.

=== This is what we do now, and it works for a single JSP page, but we cannot store the object in a request/session and have another JSP access it. We are trying to use a JSP page that includes other JSP pages because the service method got so big Java could no longer compile it.

The jsp:include is coming very close for us, but we're doing some tricks on these JSP pages that are causing us a problem with this approach.
In the main JSP, we are creating a class inside that has the data elements for that page as well as a bunch of custom routines that act on the bean data. In this case, we use a construct like:

public class BeanData
public String field1;
public String field2;
public void method1()
{ .... }

void doSomething(BeanData bean)

This has worked well. Normally, we'd create a regular bean and use jsp:useBean to access it, but we did this so we could just upload the JSP and the bean would go together for ease of update without restarting, etc. These JSP pages are bit "special" in that they are customer-defined pages that need to be changeable without having to upload a new JAR/WAR/.class file and have the application reload.
Of course, a jsp:include page cannot access the BeanData defined in the other page because the BeanData class is nested inside the servlet class created from the JSP (even if we pass it as a parameter, the receiving end will get a security access exception).

First, is there a better way for a JSP to define a class that could be accessed outside of it rather than using the <%! %> mechanism to declare the code? If not, are there any thoughts on how to pass such a private bean instance to the jsp:include page so it can also use it?
author & internet detective
Posts: 39201
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need a bean that can be compiled or just a way to store data? In Struts, people use "DynaForms" which are built at runtime. You could do this yourself with a HashMap.
I've never won anything before. Not even a tiny ad:
global solutions you can do at home or in your backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!