You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Joel Alejandro Espinosa Carra <jo...@cts-design.com> on 2006/03/23 21:45:18 UTC

ajax with struts

Hello all,

I'm coding a simple action that retrieves some data from the database in 
order to create an xml object for an ajax-based autocomplete field, I'm 
worried about the load of the database beacuse this action is called 
from a javascript event "onkeyup()" this means that the action will be 
executed every time when user press a key in the field, is there an 
other way to do this in order to increase the performance?

pd. this is not a must-have requirement but I want to know what is the 
best way to do this.

Best regards.

-- 
Ing. Joel Alejandro Espinosa Carra
CINVESTAV CTS - Centro de TecnologĂ­a de Semiconductores
Tel. +52 (33) 3770-3700 ext. 1049
http://www.cts-design.com 


-- 
Este mensaje ha sido analizado por MailScanner
en busca de virus y otros contenidos peligrosos,
y se considera que está limpio.
MailScanner agradece a transtec Computers por su apoyo.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ajax with struts

Posted by Mark Lowe <me...@gmail.com>.
On 3/23/06, Frank W. Zammetti <fz...@omnytex.com> wrote:
> Hello,
>
> The first thing I would suggest is this is one of those cases where the
> X in AJAX probably isn't appropriate.  Remember that there is NO
> requirement to use XML when doing AJAX (although I suppose it isn't AJAX
> then strictly speaking).
>
> Take Google Suggests for example.  They are not passing back XML, they
> are actually passing back, if memory serves, a chunk of Javascript that
> they execute.  I seem to recall it being just a Javascript array that is
> used to populate the dropdown, but I may not have all the details right.
>   The point though is that it isn't XML.
>
> Aside from that, one thing Google does too is they actually throttle the
> requests.  I don't have all the details, I just heard it explained once
> at a user group meeting, but basically, the faster a person types, the
> LESS requests go across, and that is dynamically calculated.
>
> Another thing you can do is write the code such that the request will
> only fire X number of milliseconds after the last keyUp is received.
> Maybe wait half a second after keyUp, and if a keyDown fires in the mean
> time, you reset the counter.  You would have to balance things so that
> the delay doesn't lead to a poor user experience, but I think that can
> be done. (I just saw Rick's reply as I was typing this, he makes the
> same basic suggestion).
>
> On the server-side, if possible, implement some sort of caching.
> Without knowing what kind of data you are using in your autocomplete, I
> can't make any specific suggestions.  But basically, if you can cache a
> subset of the most frequently used data on the server, then you won't
> have to hit the database as often.  Maybe its even possible to cache the
> entire database contents on the app server?
>
> At the end of the day there are two considerations: how many requests
> you make of the server, and what the server actually has to do to
> service the request.  If this is a LAN/WAN application, you will find
> the greater bottleneck is almost certainly what the server has to do.
> If it's Internet-based, the opposite may be true.  One bit of advice is
> to not assume you have a problem at all!  In this case you may, but do
> some basic testing first.  I've seen people assume that AJAX is horrible
> for network traffic when just the opposite wound up being true.  Then
> again, I've seen people who assumed just the opposite and got burned :)
>
> Frank
>
> Joel Alejandro Espinosa Carra wrote:
> > Hello all,
> >
> > I'm coding a simple action that retrieves some data from the database in
> > order to create an xml object for an ajax-based autocomplete field, I'm
> > worried about the load of the database beacuse this action is called
> > from a javascript event "onkeyup()" this means that the action will be
> > executed every time when user press a key in the field, is there an
> > other way to do this in order to increase the performance?

If your results aren't going to be large you could do a greedy query
on the first letter, and store the results as a javascript array
(sending the results as a javascript array may also be easier than
sending an xml). When a keyup happens on subsequent letters you could
search the javascript array rather than calling an action. There would
be a little more work on the javascript side, but you'd bother the
server less.

Mark

> >
> > pd. this is not a must-have requirement but I want to know what is the
> > best way to do this.
> >
> > Best regards.
> >
>
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> AIM: fzammetti
> Yahoo: fzammetti
> MSN: fzammetti@hotmail.com
> Java Web Parts -
> http://javawebparts.sourceforge.net
> Supplying the wheel, so you don't have to reinvent it!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ajax with struts

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Hello,

The first thing I would suggest is this is one of those cases where the 
X in AJAX probably isn't appropriate.  Remember that there is NO 
requirement to use XML when doing AJAX (although I suppose it isn't AJAX 
then strictly speaking).

Take Google Suggests for example.  They are not passing back XML, they 
are actually passing back, if memory serves, a chunk of Javascript that 
they execute.  I seem to recall it being just a Javascript array that is 
used to populate the dropdown, but I may not have all the details right. 
  The point though is that it isn't XML.

Aside from that, one thing Google does too is they actually throttle the 
requests.  I don't have all the details, I just heard it explained once 
at a user group meeting, but basically, the faster a person types, the 
LESS requests go across, and that is dynamically calculated.

Another thing you can do is write the code such that the request will 
only fire X number of milliseconds after the last keyUp is received. 
Maybe wait half a second after keyUp, and if a keyDown fires in the mean 
time, you reset the counter.  You would have to balance things so that 
the delay doesn't lead to a poor user experience, but I think that can 
be done. (I just saw Rick's reply as I was typing this, he makes the 
same basic suggestion).

On the server-side, if possible, implement some sort of caching. 
Without knowing what kind of data you are using in your autocomplete, I 
can't make any specific suggestions.  But basically, if you can cache a 
subset of the most frequently used data on the server, then you won't 
have to hit the database as often.  Maybe its even possible to cache the 
entire database contents on the app server?

At the end of the day there are two considerations: how many requests 
you make of the server, and what the server actually has to do to 
service the request.  If this is a LAN/WAN application, you will find 
the greater bottleneck is almost certainly what the server has to do. 
If it's Internet-based, the opposite may be true.  One bit of advice is 
to not assume you have a problem at all!  In this case you may, but do 
some basic testing first.  I've seen people assume that AJAX is horrible 
for network traffic when just the opposite wound up being true.  Then 
again, I've seen people who assumed just the opposite and got burned :)

Frank

Joel Alejandro Espinosa Carra wrote:
> Hello all,
> 
> I'm coding a simple action that retrieves some data from the database in 
> order to create an xml object for an ajax-based autocomplete field, I'm 
> worried about the load of the database beacuse this action is called 
> from a javascript event "onkeyup()" this means that the action will be 
> executed every time when user press a key in the field, is there an 
> other way to do this in order to increase the performance?
> 
> pd. this is not a must-have requirement but I want to know what is the 
> best way to do this.
> 
> Best regards.
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Java Web Parts -
http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ajax with struts

Posted by Rick Reumann <st...@reumann.net>.
Joel Alejandro Espinosa Carra wrote the following on 3/23/2006 3:45 PM:

> I'm worried about the load of the database beacuse this action is
> called from a javascript event "onkeyup()" this means that the action
> will be executed every time when user press a key in the field, is
> there an other way to do this in order to increase the performance?

When you capture onKeyUp() start some sort of timer, so that the user 
has to pause for at least maybe 2 seconds(?) before you hit the server. 
This way if the user is just typing along quickly that ajax event won't 
fire.


-- 
Rick
http://www.learntechnology.net

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org