You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Robert Balahura <rj...@sciborg.uwaterloo.ca> on 2000/06/19 09:19:23 UTC

Struts Design Question - New User

Hello struts users,

I am just getting my feet wet using the framework and I want to ask for
advice to help get started.

I have listed below my beginning architecture for one task and then a
question afterwards. The task is to display a list of products from my
database based on a user's query.  This is the architecture so far.  [I am
starting out using my ActionClass also as my business logic bean for this
test, but I will separate them later]

The HTML form is submitted to the ActionServlet controller, the
ActionServlet controller instantiates an ActionClass based on the action.xml
mapping.  My ActionClass takes a connection from my connection pool (taken
from ServletContext).  My ActionClass then performs the query to the
database and gets a "Collection" of "Product" objects that satisfies the
query.  Let's say there are 40 "Products" in the "Collection" as a result of
this query. I then put this "Collection" in the "Session" and forward
control to the appropriate JSP page to display the "Collection" of
"Products".

My questions:
What is a good technique if I only want to display 10 Products per page?
Would I resubmit to the ActionServlet after a "Next" button is pushed, or
would I submit to the same JSP page itself and manipulate a catalog index
inside the JSP page using the same object?

Is it OK to store the entire "Collection" of "Products" in my session
object, or what are the alternatives?


Thank-you for any assistance,

Rob


Re: Struts Design Question - New User

Posted by Nina Nussbaum-Jones <ni...@lmco.com>.
This is sort of a comment/question.  I have been subscribed to this list
for an entire afternoon.  And I haven't said a word! (Imagine that...)
But I cannot hold back now.  (Sorry -- sarcasm makes me giggle,
and giggling makes programming so much more fun.)

Okay.  Seriously, this is the quintessential data access problem
that every single site on the internet has to contend with:  Should
we page?  If so, should the user have control?  If so, should we
buffer?  Yes?  Well, then let's break it into sets of 10.  But what
if the user hates clicking next and is willing to wait for all the
results to do a Search on the text content?  ...and so on.

I am wondering if anyone has come up with a clean "PAGING"
design pattern/taglib/algorithm (I know that dates me)/or other
"standard" way of packaging query results.   Has any work been
done (either successful or otherwise) on this topic that you know of?
We are still struggling at our site to define a "best practices" or
rules of thumb approach.

Thanks for enduring!
-Nina-

Robert Balahura wrote:

> Thanks also Craig.  That is also really helpful. I will try this. :)
>
> -----Original Message-----
> From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
> Sent: Monday, June 19, 2000 12:54 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Struts Design Question - New User
>
> Robert Balahura wrote:
>
> > Hello struts users,
> >
> > I am just getting my feet wet using the framework and I want to ask for
> > advice to help get started.
> >
> > I have listed below my beginning architecture for one task and then a
> > question afterwards. The task is to display a list of products from my
> > database based on a user's query.  This is the architecture so far.  [I am
> > starting out using my ActionClass also as my business logic bean for this
> > test, but I will separate them later]
> >
>
> Good :-)
>
> >
> > The HTML form is submitted to the ActionServlet controller, the
> > ActionServlet controller instantiates an ActionClass based on the
> action.xml
> > mapping.  My ActionClass takes a connection from my connection pool (taken
> > from ServletContext).  My ActionClass then performs the query to the
> > database and gets a "Collection" of "Product" objects that satisfies the
> > query.  Let's say there are 40 "Products" in the "Collection" as a result
> of
> > this query. I then put this "Collection" in the "Session" and forward
> > control to the appropriate JSP page to display the "Collection" of
> > "Products".
> >
>
> Sounds like a reasonable approach.  If everything fits into memory, you
> might
> look at the <struts:enumerate> tag when you are generating the actual
> display.
> The example application uses this on the "registration.jsp" page to list all
> the
> current subscriptions for a user.
>
> >
> > My questions:
> > What is a good technique if I only want to display 10 Products per page?
> > Would I resubmit to the ActionServlet after a "Next" button is pushed, or
> > would I submit to the same JSP page itself and manipulate a catalog index
> > inside the JSP page using the same object?
> >
> > Is it OK to store the entire "Collection" of "Products" in my session
> > object, or what are the alternatives?
> >
>
> If you have enough memory to keep the retrieved data around, that is by far
> the
> simplest approach.  Using this direction, you might consider keeping a
> separate
> session variable that keeps track of the starting index in the collection to
> be
> displayed this time.  Then, either the "Next" or "Previous" button would go
> to
> an action that incremented or decremented the starting index by 10 (with the
> obvious checks for falling off the end of the list), before forwarding to
> the
> JSP page that does the display.
>
> If you have too many entries (or too many users) to keep the entire
> collection
> in memory, the most common approaches to this problem are:
>
> * Re-execute the query each time, then throw away the first "n" entries
>   you don't want to display.
>
> * Re-execute the query each time, but modify the WHERE clause to grab
>   only the rows you want.
>
> In either case, you would only keep the 10 rows you want to display --
> probably
> as a request attribute instead of a session attribute because you do not
> need
> them after you are through generating the current page.
>
> >
> > Thank-you for any assistance,
> >
> > Rob
>
> Craig McClanahan


RE: Struts Design Question - New User

Posted by Robert Balahura <rj...@sciborg.uwaterloo.ca>.
Thanks also Craig.  That is also really helpful. I will try this. :)

-----Original Message-----
From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
Sent: Monday, June 19, 2000 12:54 PM
To: struts-user@jakarta.apache.org
Subject: Re: Struts Design Question - New User


Robert Balahura wrote:

> Hello struts users,
>
> I am just getting my feet wet using the framework and I want to ask for
> advice to help get started.
>
> I have listed below my beginning architecture for one task and then a
> question afterwards. The task is to display a list of products from my
> database based on a user's query.  This is the architecture so far.  [I am
> starting out using my ActionClass also as my business logic bean for this
> test, but I will separate them later]
>

Good :-)

>
> The HTML form is submitted to the ActionServlet controller, the
> ActionServlet controller instantiates an ActionClass based on the
action.xml
> mapping.  My ActionClass takes a connection from my connection pool (taken
> from ServletContext).  My ActionClass then performs the query to the
> database and gets a "Collection" of "Product" objects that satisfies the
> query.  Let's say there are 40 "Products" in the "Collection" as a result
of
> this query. I then put this "Collection" in the "Session" and forward
> control to the appropriate JSP page to display the "Collection" of
> "Products".
>

Sounds like a reasonable approach.  If everything fits into memory, you
might
look at the <struts:enumerate> tag when you are generating the actual
display.
The example application uses this on the "registration.jsp" page to list all
the
current subscriptions for a user.

>
> My questions:
> What is a good technique if I only want to display 10 Products per page?
> Would I resubmit to the ActionServlet after a "Next" button is pushed, or
> would I submit to the same JSP page itself and manipulate a catalog index
> inside the JSP page using the same object?
>
> Is it OK to store the entire "Collection" of "Products" in my session
> object, or what are the alternatives?
>

If you have enough memory to keep the retrieved data around, that is by far
the
simplest approach.  Using this direction, you might consider keeping a
separate
session variable that keeps track of the starting index in the collection to
be
displayed this time.  Then, either the "Next" or "Previous" button would go
to
an action that incremented or decremented the starting index by 10 (with the
obvious checks for falling off the end of the list), before forwarding to
the
JSP page that does the display.

If you have too many entries (or too many users) to keep the entire
collection
in memory, the most common approaches to this problem are:

* Re-execute the query each time, then throw away the first "n" entries
  you don't want to display.

* Re-execute the query each time, but modify the WHERE clause to grab
  only the rows you want.

In either case, you would only keep the 10 rows you want to display --
probably
as a request attribute instead of a session attribute because you do not
need
them after you are through generating the current page.

>
> Thank-you for any assistance,
>
> Rob

Craig McClanahan



Re: Struts Design Question - New User

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Robert Balahura wrote:

> Hello struts users,
>
> I am just getting my feet wet using the framework and I want to ask for
> advice to help get started.
>
> I have listed below my beginning architecture for one task and then a
> question afterwards. The task is to display a list of products from my
> database based on a user's query.  This is the architecture so far.  [I am
> starting out using my ActionClass also as my business logic bean for this
> test, but I will separate them later]
>

Good :-)

>
> The HTML form is submitted to the ActionServlet controller, the
> ActionServlet controller instantiates an ActionClass based on the action.xml
> mapping.  My ActionClass takes a connection from my connection pool (taken
> from ServletContext).  My ActionClass then performs the query to the
> database and gets a "Collection" of "Product" objects that satisfies the
> query.  Let's say there are 40 "Products" in the "Collection" as a result of
> this query. I then put this "Collection" in the "Session" and forward
> control to the appropriate JSP page to display the "Collection" of
> "Products".
>

Sounds like a reasonable approach.  If everything fits into memory, you might
look at the <struts:enumerate> tag when you are generating the actual display.
The example application uses this on the "registration.jsp" page to list all the
current subscriptions for a user.

>
> My questions:
> What is a good technique if I only want to display 10 Products per page?
> Would I resubmit to the ActionServlet after a "Next" button is pushed, or
> would I submit to the same JSP page itself and manipulate a catalog index
> inside the JSP page using the same object?
>
> Is it OK to store the entire "Collection" of "Products" in my session
> object, or what are the alternatives?
>

If you have enough memory to keep the retrieved data around, that is by far the
simplest approach.  Using this direction, you might consider keeping a separate
session variable that keeps track of the starting index in the collection to be
displayed this time.  Then, either the "Next" or "Previous" button would go to
an action that incremented or decremented the starting index by 10 (with the
obvious checks for falling off the end of the list), before forwarding to the
JSP page that does the display.

If you have too many entries (or too many users) to keep the entire collection
in memory, the most common approaches to this problem are:

* Re-execute the query each time, then throw away the first "n" entries
  you don't want to display.

* Re-execute the query each time, but modify the WHERE clause to grab
  only the rows you want.

In either case, you would only keep the 10 rows you want to display -- probably
as a request attribute instead of a session attribute because you do not need
them after you are through generating the current page.

>
> Thank-you for any assistance,
>
> Rob

Craig McClanahan



RE: Struts Design Question - New User

Posted by Robert Balahura <rj...@sciborg.uwaterloo.ca>.
Thanks Kevin, very helpful. :)

-----Original Message-----
From: Kevin Duffey [mailto:kduffey@buymedia.com]
Sent: Monday, June 19, 2000 12:45 PM
To: struts-user@jakarta.apache.org
Subject: RE: Struts Design Question - New User


Hi,

> What is a good technique if I only want to display 10 Products per page?
> Would I resubmit to the ActionServlet after a "Next" button is pushed, or
> would I submit to the same JSP page itself and manipulate a catalog index
> inside the JSP page using the same object?

There are probably a 1000 ways to do this..but I can think of two ways I
would do it. Not sure if its the right way..

1) Like you said..resubmit to the jsp page. In scriplet code, set up a
counter, and store it in the session as its own object. If your using an int
type, you'll have to wrap it in an Integer class.

<%

  int i = 0;

  if( session.getAttribute("MyCounter") != null )
    i = ( (Integer) session.getAttribute("MyCounter")).intValue();

  int i2 = i;
  i += 10; // increment by 10

  session.setAttribute("MyCounter", new Integer(i) );

  for( int cntr = i2; cntr < i; cntr++ )
  {
    .. do your work in here to show the next 10 items
  }

%>



The second way is almost identical to the above, but would use a JSP 1.1
taglib instead. You might have a taglib in the JSP page like:

<mytaglib:itemDisplay displayCount="10" counterName="MyCounter" />

There are many options you could probably do with a taglib. The above
assumes you pass in the number of items you want to display, and the rest of
the code would be in the taglib class file, which would get the session
attribute "MyCounter", and basically do the above. Anyways..I am not sure if
the above works perfectly..I didn't test it..just wrote it in. I hope my
Java is good enough to see that work. ;)

> Is it OK to store the entire "Collection" of "Products" in my session
> object, or what are the alternatives?

Heh..I had a hard time understanding how Java dealt with objects for a long
time. As it turns out..what is stored in the HttpSession is a "reference" (a
4-byte pointer in C/C++ terms) to the object which is somewhere in memory.
So, your not actually storing the entire object in the HttpSession..just a
reference to it. All objects in Java use reference counting to keep track of
their use. When the counter == 0, the object is good for garbage collection.
So, for example, if you do the following:

Object o = new Object();

session.setAttribute("MyObject", o);

o = null;


The object you just created is actually still in existence. The reason is,
you created it and pointed the "o" at the object (a reference to that object
in memory somewhere). Then you stored it in the session (HttpSession). That
sets another reference to the object in memory. So now the new object has 2
references to it. When you do the o = null; in most programming languages,
the newly created object would be lost in memory, but with Java, because of
the HttpSession reference to it, it still exists, and is attainable in code.
The way, in this case, to finally get the Object to be gc'd (garbage
collected), is to do a session.removeAttribute("MyObject") which will remove
the object from the session..decreasing the reference count of that object
by one. If nothing else references it, the object is marked for gc.

So, it is perfectly safe to store the entire collection in the
HttpSession..it will only take 4 bytes. Actually..I am not sure of the
"reference" size in Java. It may be 8-byte pointers they use..to work on all
platforms. I think it is based on the memory model of the platform. Wintel
is usually 32-bit, so 4-byte poiters. But Solaris on Sun would use 8-byte
pointers I would think.


Alrighty..hope that helped.


RE: Struts Design Question - New User

Posted by Kevin Duffey <kd...@buymedia.com>.
Hi,

> What is a good technique if I only want to display 10 Products per page?
> Would I resubmit to the ActionServlet after a "Next" button is pushed, or
> would I submit to the same JSP page itself and manipulate a catalog index
> inside the JSP page using the same object?

There are probably a 1000 ways to do this..but I can think of two ways I
would do it. Not sure if its the right way..

1) Like you said..resubmit to the jsp page. In scriplet code, set up a
counter, and store it in the session as its own object. If your using an int
type, you'll have to wrap it in an Integer class.

<%

  int i = 0;

  if( session.getAttribute("MyCounter") != null )
    i = ( (Integer) session.getAttribute("MyCounter")).intValue();

  int i2 = i;
  i += 10; // increment by 10

  session.setAttribute("MyCounter", new Integer(i) );

  for( int cntr = i2; cntr < i; cntr++ )
  {
    .. do your work in here to show the next 10 items
  }

%>



The second way is almost identical to the above, but would use a JSP 1.1
taglib instead. You might have a taglib in the JSP page like:

<mytaglib:itemDisplay displayCount="10" counterName="MyCounter" />

There are many options you could probably do with a taglib. The above
assumes you pass in the number of items you want to display, and the rest of
the code would be in the taglib class file, which would get the session
attribute "MyCounter", and basically do the above. Anyways..I am not sure if
the above works perfectly..I didn't test it..just wrote it in. I hope my
Java is good enough to see that work. ;)

> Is it OK to store the entire "Collection" of "Products" in my session
> object, or what are the alternatives?

Heh..I had a hard time understanding how Java dealt with objects for a long
time. As it turns out..what is stored in the HttpSession is a "reference" (a
4-byte pointer in C/C++ terms) to the object which is somewhere in memory.
So, your not actually storing the entire object in the HttpSession..just a
reference to it. All objects in Java use reference counting to keep track of
their use. When the counter == 0, the object is good for garbage collection.
So, for example, if you do the following:

Object o = new Object();

session.setAttribute("MyObject", o);

o = null;


The object you just created is actually still in existence. The reason is,
you created it and pointed the "o" at the object (a reference to that object
in memory somewhere). Then you stored it in the session (HttpSession). That
sets another reference to the object in memory. So now the new object has 2
references to it. When you do the o = null; in most programming languages,
the newly created object would be lost in memory, but with Java, because of
the HttpSession reference to it, it still exists, and is attainable in code.
The way, in this case, to finally get the Object to be gc'd (garbage
collected), is to do a session.removeAttribute("MyObject") which will remove
the object from the session..decreasing the reference count of that object
by one. If nothing else references it, the object is marked for gc.

So, it is perfectly safe to store the entire collection in the
HttpSession..it will only take 4 bytes. Actually..I am not sure of the
"reference" size in Java. It may be 8-byte pointers they use..to work on all
platforms. I think it is based on the memory model of the platform. Wintel
is usually 32-bit, so 4-byte poiters. But Solaris on Sun would use 8-byte
pointers I would think.


Alrighty..hope that helped.