aspose file tools*
The moose likes JSP and the fly likes Creating a class within JSP to be shared among pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Creating a class within JSP to be shared among pages" Watch "Creating a class within JSP to be shared among pages" New topic
Author

Creating a class within JSP to be shared among pages

David Wall
Greenhorn

Joined: Jun 06, 2005
Posts: 1
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?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

David,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a class within JSP to be shared among pages