Link to home
Start Free TrialLog in
Avatar of Bekkerus
Bekkerus

asked on

Page calling singleton object, not working....

hello experts,

 New to JSP. I made a singleton object that I want to access in a JSP page but I am getting errors and I do not know how to fix. How can I access this in my jsp page?

Here is the page:

<%@ page import="com.avalon.pmc.*" %>
<%@ page import="java.util.*" %>


<HTML>
<HEAD>
<TITLE>TEST</TITLE>
</HEAD>
<BODY>

<%

public static ArrayList LocationList = new ArrayList();
LocationList = WorkLocContainer.getWorkLocationList(); // returns arrayList
for (int i = 0; i < LocationList.size(); i++)
{
     WorkLocation loc = new WorkLocation();
     loc  = (WorkLocation) LocationList.get(i);
     out.println("<B>" + loc.getWorkLocation() + " </B><BR>");
}

%>

</BODY>
</HTML>

here is the error:

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\Standalone\localhost\avalon\jsp\Default$jsp.java:60: '}' expected.
                out.write("\r\n\r\n\t");
                                        ^


An error occurred between lines: 4 and 16 in the jsp file: /jsp/Default.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\Standalone\localhost\avalon\jsp\Default$jsp.java:66: Statement expected.
                            public static ArrayList LocationList = new ArrayList();
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You cannot declare a member variable (static or not) inside a jsp page like that. When the jsp page is converted to a servlet it results in a member variable definition inside a method.
Also immediately after you declare it you retrieve the array so I don't see the purpose of having it static, so just defining it as a local var should be enough:

ArrayList LocationList = WorkLocContainer.getWorkLocationList(); // returns arrayList

If you need the list to be globally accessible to all pages then use an application scope bean:

<jsp:useBean id="LocationList" class="java.util.ArrayList" scope="application"/>
Avatar of Bekkerus
Bekkerus

ASKER

Objects,

 The reason I did that this because I made a singleton object that contains workLocations inside the WorkLocContainer. This reduces the loading time from the database and reduces that number of objects in memory significantly.

 I was hoping to use this object within the jsp pages. Can I load the data from the singleton WorkLocContainer into a scope bean? How would I do that?

<jsp:useBean id="LocationList" class="java.util.ArrayList" scope="application"/>

<%
 LocationList = WorkLocContainer.getWorkLocationList();
%>
No need then, just define it as a local var when needed:

<%

ArrayList LocationList = WorkLocContainer.getWorkLocationList();

%>
Objects,

 Yeah but then there is no true global objects that all users use. It would be a global object for that single session and all other users make their own. That sucks. There is no way of implementing a singleton object in JSP  then, right? Is there no way to reduce the over head of creating thousands of identical objects? I am hoping there is some way to do this.

I thought you said you already created one:

"..because I made a singleton object that contains workLocations inside the WorkLocContainer."

The variable LocationList is just a local reference to your singleton object.
Objects,

 Sort of. I have a servlet "Controller.java" That started the singleton "WorkLocContainer.java" then in the Default.jsp I make the session arrayList...

<jsp:useBean id="LocationList" class="java.util.ArrayList" scope="application"/>

Then when I try and call:

ArrayList LocationList = WorkLocContainer.getWorkLocationList(); //to get from the static class an arraylist

It fails. Says cannot reference a static class in a non-static page...

So I guess that means jsp pages are designed to not allow the sharing of objects and reducing overhead, right?
I don't see why you define a bean, and then declare a variable of the same name. If getWorkLocationList() is a static method returning the singleton instance then I see no need for using a bean.
Without seeing the source for WorkLocContainer it is hard to say the cause of the error.

> So I guess that means jsp pages are designed to not
> allow the sharing of objects and reducing overhead, right?

Not really, jsp provides means of sharing objects by defining the scope of the bean, and/or using the session.
And you can also use any of the techniques available to Java for object sharing (as you appear to already be doing).
ASKER CERTIFIED SOLUTION
Avatar of nex1999
nex1999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> . Then you can call its methods something like this.

isn't that what I already said :)
in fact all u need is:

WorkLocContainer wk = WorkLocContainer.getInstance();
ArrayList LocationList = wk.getWorkLocationList();



Thats exactly what I had already said.

Comment from objects  12/29/2002 12:49PM PST  
You cannot declare a member variable (static or not) inside a jsp page like that. When the jsp page is converted to a servlet it results in a member variable definition inside a method.
Also immediately after you declare it you retrieve the array so I don't see the purpose of having it static, so just defining it as a local var should be enough:

ArrayList LocationList = WorkLocContainer.getWorkLocationList(); // returns arrayList

 

Comment from objects  12/29/2002 02:23PM PST  
No need then, just define it as a local var when needed:

<%

ArrayList LocationList = WorkLocContainer.getWorkLocationList();

%>