aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Updating a JSP session attribute and html code on mouse click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Updating a JSP session attribute and html code on mouse click" Watch "Updating a JSP session attribute and html code on mouse click" New topic
Author

Updating a JSP session attribute and html code on mouse click

Joel Sander
Ranch Hand

Joined: Jan 09, 2009
Posts: 38
Hi all,

I feel dumb asking this. bit would really appreciate any help!

I have a JSP page that has a few <input src=..." id ="..." onclick="..."> entries...when one of them is clicked on I would like to attempt to update a session variable and if the update is successful, update the html display which is based on one of the session attributes. What is the simplest way to do this? Is it possible to do this without consuming bandwidth in reloading the page? Thanks so much,

Joel
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Ajax is the technology you seek.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joel Sander
Ranch Hand

Joined: Jan 09, 2009
Posts: 38
Thank you Bear!

Always another thing to learn to make my code do what I want...I'll go look for a book on the subject...

Any suggestions before I head to Borders?

Edit:

Any there any places online that you would recommend that might give me a few points or hints?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Well yeah. I suggest you use jQuery for your Ajax library and suggest that you buy my book on it.

Seriously, you can probably pick up what you need online, but if you really want to learn how to do it well, then spring for the book.
Joel Sander
Ranch Hand

Joined: Jan 09, 2009
Posts: 38
LOL...

I'll take a look around online first I guess...I don't really need to be an expert...just want to get one or two apps off the ground...

Do you know of any books that are good for a newbie and that give full code examples? (Books with code snippet examples seem to leave out pieces that I'm missing.)

Thanks so much, Bear!

---

Edit:

In looking up AJAX, I'm seeing some stuff about activeX...do I need to learn this first? I sure hope not!
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
No need to learn AJAX for this , upon onclick , call a function which will be making a call to the jsp/servlet and then increase your count there


If you want something you never had do something which you had never done
Joel Sander
Ranch Hand

Joined: Jan 09, 2009
Posts: 38
RaviNada Kiran wrote:No need to learn AJAX for this , upon onclick , call a function which will be making a call to the jsp/servlet and then increase your count there


That sounds like what I am trying to do...Would it be possible to get a short example? I spent about half a day trying to get a simple one to work...and probably tried all the wrong things...
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Hi Joel ,

do you know about the concept of the HttpSessionListener , why don't you think that way , this will be internally calling some function which is responsible to do database activity.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
RaviNada Kiran wrote:No need to learn AJAX for this , upon onclick , call a function which will be making a call to the jsp/servlet and then increase your count there


Your solution sounds like you can just update a counter on the server without an http call.

Joel is going to have to make an HTTP call to the server in some way unless he hires someone to stand behind every user of the application and record things with paper and pencil.

  • It can be an Ajax call
  • It can be by adding an image to the page
  • It can be by adding a script tag to the page
  • It can be opening a url in an iframe
  • It can be posting a form to a hidden iframe
  • It can be opening page in a pop up window
  • It can be posting back the entire page


  • Eric
    Joel Sander
    Ranch Hand

    Joined: Jan 09, 2009
    Posts: 38
    RaviNada Kiran wrote:Hi Joel ,

    do you know about the concept of the HttpSessionListener , why don't you think that way , this will be internally calling some function which is responsible to do database activity.


    I was trying to figure out how to do something like this but didn't have any success...Can you point me to a simple example? On the click, I'll be doing something like running a simple validation code and then incrementing a number that is displayed on the page...
    Joel Sander
    Ranch Hand

    Joined: Jan 09, 2009
    Posts: 38
    Eric Pascarello wrote:
    RaviNada Kiran wrote:No need to learn AJAX for this , upon onclick , call a function which will be making a call to the jsp/servlet and then increase your count there


    Your solution sounds like you can just update a counter on the server without an http call.

    Joel is going to have to make an HTTP call to the server in some way unless he hires someone to stand behind every user of the application and record things with paper and pencil.

  • It can be an Ajax call
  • It can be by adding an image to the page
  • It can be by adding a script tag to the page
  • It can be opening a url in an iframe
  • It can be posting a form to a hidden iframe
  • It can be opening page in a pop up window
  • It can be posting back the entire page


  • Eric


    So your saying that on every click, the page would have to be resubmitted to the server for the validation method to be run, then the number to be incremented, and the the updated entire page would be posted back? That sounds horribly inefficient...I was hoping to allow the user to make a variety of clicks (say on "+" or "-" buttons). At the end of all the clicks, when the user is happy with their selection, to allow the user to click on a submit changes button.

    To be honest, I really don't know the right way to do this...I'm hoping JSP will do this since I have some knowledge of it...but maybe the right way is using something else like a blend of JSP and javascript?
    RaviNada Kiran
    Ranch Hand

    Joined: Jan 30, 2009
    Posts: 528
    Joel ,


    what you want to update to the sessionvariable ??tell me fast its 12:30 and i am still waken

    RaviNada Kiran
    Ranch Hand

    Joined: Jan 30, 2009
    Posts: 528
    This may help you :

    import javax.servlet.http.*;
    public class SessionCount implements HttpSessionListener{
    private static int numberOfSessionsCount = 0;
    public void sessionCreated (HttpSessionEvent evt){
    numberOfSessionsCount++;
    }
    public void sessionDestroyed (HttpSessionEvent evt){
    numberOfSessionsCount--;
    }
    // here is our own method to return the number of current sessions
    public static int getNumberOfSessionsCount(){
    return numberOfSessionsCount;
    }
    }


    //This is inside JSP
    <html>
    <body>
    <h1>Session Counter</h1>
    There are
    <%=SessionCount.getNumberOfSessionsCount()%>
    active sessions in the server.
    </body>
    </html>

    sleeping now
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61010
        
      65

    Why on earth would someone submit and have to repaint the whole page just to touch the session?
    Joel Sander
    Ranch Hand

    Joined: Jan 09, 2009
    Posts: 38
    Bear Bibeault wrote:Why on earth would someone submit and have to repaint the whole page just to touch the session?


    That is what I don't want to have happen...or at least one of the things I don't want to have happen...
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61010
        
      65

    Then you do not want to follow the advice that says to do so.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61010
        
      65

    Also, please do not use code that puts old-fashioned scriptlets in a JSP as a template or as a proper example of how to do things.
    RaviNada Kiran
    Ranch Hand

    Joined: Jan 30, 2009
    Posts: 528
    Task can be done in anyway and each one will have their own idea . and no body is perfect in java/j2EE
     
     
    subject: Updating a JSP session attribute and html code on mouse click