Page calling singleton object, not working....

Bekkerus
Bekkerus used Ask the Experts™
on
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();
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
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"/>

Author

Commented:
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();
%>
Mick BarryJava Developer
Top Expert 2010

Commented:
No need then, just define it as a local var when needed:

<%

ArrayList LocationList = WorkLocContainer.getWorkLocationList();

%>
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Author

Commented:
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.

Mick BarryJava Developer
Top Expert 2010

Commented:
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.

Author

Commented:
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?
Mick BarryJava Developer
Top Expert 2010

Commented:
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).
Commented:
Since WorkLocContainer is a singleton you have to make an instance of the WorkLocContainer 1st. Then you can call its methods something like this.

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

You dont need to use session object if you dont want to.
Mick BarryJava Developer
Top Expert 2010

Commented:
> . 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();



Mick BarryJava Developer
Top Expert 2010

Commented:
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();

%>
 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial