Options for storing variables/information in a web application

B

bort

Hi all

If I need to have some information available to a user on a website across
multiple pages, I usually just store it in a session variable. For example,
the contents of a drop down box will be stored as a Vector in the session as
an attribute.

However, as you can guess, when you have multiple users coming to your site,
this can cause severe memory use, since each user (and thus, each session)
has memory allocated for this object.

I'm guessing that there is a way to store this kind of 'common' information
such that it is available to all visiting users without creating a copy of
it for all visiting users. Perhaps as a web application variable?

Any ideas?
bort
 
N

Neill

bort said:
Hi all

If I need to have some information available to a user on a website across
multiple pages, I usually just store it in a session variable. For example,
the contents of a drop down box will be stored as a Vector in the session as
an attribute.

However, as you can guess, when you have multiple users coming to your site,
this can cause severe memory use, since each user (and thus, each session)
has memory allocated for this object.

I'm guessing that there is a way to store this kind of 'common' information
such that it is available to all visiting users without creating a copy of
it for all visiting users. Perhaps as a web application variable?

Any ideas?
bort


How about declaring it as a static variable in a helper class, and add the
class to the import statement? Then make a call similar to:

Vector v = MyHelperClass.MY_VECTOR;
 
S

Sudsy

bort wrote:
I'm guessing that there is a way to store this kind of 'common' information
such that it is available to all visiting users without creating a copy of
it for all visiting users. Perhaps as a web application variable?

Any ideas?
bort

You've already arrived at the solution: store it in application context.
You're provided a ServletConfig argument to the init method of your
HttpServlet. Invoke getServletContext on that and then setAttribute on
the returned ServletContext.
 
B

bort

So, I could stored my Vectors in the ServletContext...

HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
context.setAttribute("varname", new Vector());

.... and then I'd only have one instance of this Vector (thus not duplicating
data across the memory space)?

When I was using HttpSession to store these variables, I'd use

session.getAttribute("varname");

to get to my Vector. How do I get to it now (from within a JSP)?

bort
 
S

Sudsy

bort wrote:
to get to my Vector. How do I get to it now (from within a JSP)?

Within JSP, all contexts (page, request, session and application) are
searched to resolve the reference. You don't need to explicity specify
it. Browse the source and prove it to yourself.
 
M

Murray

bort said:
So, I could stored my Vectors in the ServletContext...

HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
context.setAttribute("varname", new Vector());

That's a pretty round-about way of getting it. Assuming you're using a
generic HttpServlet, you should be able to just call getServletContext()
directly.
 
H

Hal Rosser

I heard somewhere that its better practice to pass what you need to the next
page.
 
R

Russ Perry Jr

Within JSP, all contexts (page, request, session and application) are
searched to resolve the reference. You don't need to explicity specify
it. Browse the source and prove it to yourself.

Hi Sudsy! :)

Got a similar question for you...

Let's say I just made a class with a bunch of strings, but since I
only need one instance of it, I decided not to bother making a
vector of one, and just instantiated an object from the class and
put it in session scope. Then, in my JSP, I want to use bean:write
tags to display the information from the object. But I keep getting
an error to the effect that there's "No getter method for XXXX in
YYYY" where XXXX is a member/field in my class (which DOES have a
getter method, you'd better believe it) and YYYY is the name I
tried to give the object with a bean:define, and a few other things,
or just the attribute name that I gave it. Neither works. I even
tried to make a vector with the one object anyway, and that gave me
the same error with a logic:iterate tag (?!?). The only thing I
can think is that this JSP doesn't have an ActionForm since it's
output only, but otherwise I'm a bit stumped. I spent a fair
amount of time today trying various things (jsp:useBean,
bean:define, bean:????, ...) and trying to find examples to no
avail. Any ideas?
 
S

Sudsy

Russ Perry Jr wrote:
Hi Sudsy! :)

Got a similar question for you...

Let's say I just made a class with a bunch of strings ...<snip>
...I spent a fair
amount of time today trying various things (jsp:useBean,
bean:define, bean:????, ...) and trying to find examples to no
avail. Any ideas?

For you, two mechanisms:

<%@ taglib uri="bean" prefix="bean" %>
<head>
<title>Demo page</title>
</head>
<%
String exampleString = "This is a string";
pageContext.setAttribute( "myString", exampleString );
%>
This is one way: <bean:write name="myString" />
<br>
Here's another: <%= exampleString %>
</body>

Try it out in your favorite servlet engine. The results should be to
your liking.
 
S

Sudsy

Sudsy said:
Russ Perry Jr wrote:
<snip>
<%@ taglib uri="bean" prefix="bean" %>
<head>
<title>Demo page</title>
</head>
<%
String exampleString = "This is a string";
pageContext.setAttribute( "myString", exampleString );
%>
This is one way: <bean:write name="myString" />
<br>
Here's another: <%= exampleString %>

<br>
<bean:define id="anotherString" value="This is another string" />
Finally: said:

....in the interest of completeness. Note that all of this is
covered in the javadocs for the Struts tag libraries. It even
includes the detail that the type for bean:define defaults to
java.lang.String when you specify the value attribute.
Of course I've got all the javadoc pages bookmarked in my
browser...something for you to consider?
 
R

Russ Perry Jr

Sudsy said:
Note that all of this is covered in the javadocs for the Struts tag
libraries.

I'd been through them, and couldn't find a clear answer to my dilemna.
Or more to say, I couldn't find a clear example.

Now, it seems that it was just an issue with the case of the field names.
The problem is, there's still at least one member it can't find a getter
method for.

The field is named something like xYZField, and there is a method named
getXYZField, but I'm getting an error that:
No getter method for property xYZField of bean MyInfo

....which is what I was getting for other fields before I fixed them.
Is there something weird about a field with only one lower-case letter?
 
S

Sudsy

Russ Perry Jr wrote:
The field is named something like xYZField, and there is a method named
getXYZField, but I'm getting an error that:




...which is what I was getting for other fields before I fixed them.
Is there something weird about a field with only one lower-case letter?

It looks fine to me, but then this isn't the real source code, is it?
You already know about the case switching so I don't see how I can
assist you further. Well, not without the source...
 
R

Russ Perry Jr

It looks fine to me, but then this isn't the real source code, is it?

No, but it's pretty accurately transcribed, given how many times I
looked at it. The solution was to just get rid of the xYZ part.
You already know about the case switching so I don't see how I can
assist you further. Well, not without the source...

I'm past it now, but thanks for looking again. We've actually made
some pretty strides on the site, though there are lots of little
lingering TODOs... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top