Best way to store variables and user choices in JSP page

Discussion in 'Java' started by Vajra, Oct 4, 2007.

  1. Vajra

    Vajra Guest

    Hello everybody

    I am working on a Website built on JSP and Servlets . one of the features of
    the site is to give user the chance to subscribe to different newsletters
    under different categories.

    So for example, user1 surfs to the page which displays category sport and
    under that some options (check boxes) and he can choose zero or more, and he
    clicks to add the next catetory options , for example entertainement.


    I am new at Java and I'd be grateful if you help me with my questions:

    1. What is the best way to keep user's choices as he surfs and adds options
    under each category ? I thought of using session vars , in form of arrays,
    but I wonder if there is a better more efficient way ?

    2. At the last page, user is shown the category names with two links beside
    them , edit and remove (he will not see the individual choices under each
    category) ... could you also advise what is the best way to implement this
    as well? For example if I use array session vars, I am not sure how to
    display that particular page again with the choices made...


    From the summary page , something like this is displayed to user if he has
    choices from 3 categoires of Sport , Social, Home ; but he did not choose
    anything from Movies and others.:

    Sport : edit (hyperlink) , remove (hyperlink)
    Social : edit (hyperlink) , remove (hyperlink)
    Home edit (hyperlink) , remove (hyperlink)


    The user should have a choice to go back to each category page and edit it
    (he will see his previous choices in check boxes), or just by clicking a
    link in the final summary page, to remove it. The categories and options
    under each are read from database. User's choices are not commited to DB
    until he reviews the summary and clicks confirm button at the last stage.

    Thank you in advance
    Vajra
     
    Vajra, Oct 4, 2007
    #1
    1. Advertising

  2. Vajra

    Guest

    Well first you should not be using session variables. You can use
    hidden fields avaialble in html
    <input type="hidden" name="" value-""> , By using hidden fields you
    will be able to get all the values in each page you just need to get
    all this values and store it back to the returning page.

    For the last page editing thing there can be 2 ways to do it

    1) Create a link in the jsp page in such a way that all the options
    which they have selected goes with it

    eg: Sports <a href="Test.jsp?
    sports=abc,cde&social=xyz,wxy&option=edit&category=sports"> Edit </a>

    2) second option is whenever the link is clicked you call a
    javascript function which will do a post submit of the page and will
    set a value of some variable which will tell whether it was edit or
    remove and which category

    Sports <a href="#" onClick="DoSomething('Edit','Sports')" > Edit </a>

    <script>

    function DoSomething(action , category)
    {
    .............

    }

    </script>



    On Oct 4, 2:16 am, "Vajra" <> wrote:
    > Hello everybody
    >
    > I am working on a Website built on JSP and Servlets . one of the features of
    > the site is to give user the chance to subscribe to different newsletters
    > under different categories.
    >
    > So for example, user1 surfs to the page which displays category sport and
    > under that some options (check boxes) and he can choose zero or more, and he
    > clicks to add the next catetory options , for example entertainement.
    >
    > I am new at Java and I'd be grateful if you help me with my questions:
    >
    > 1. What is the best way to keep user's choices as he surfs and adds options
    > under each category ? I thought of using session vars , in form of arrays,
    > but I wonder if there is a better more efficient way ?
    >
    > 2. At the last page, user is shown the category names with two links beside
    > them , edit and remove (he will not see the individual choices under each
    > category) ... could you also advise what is the best way to implement this
    > as well? For example if I use array session vars, I am not sure how to
    > display that particular page again with the choices made...
    >
    > From the summary page , something like this is displayed to user if he has
    > choices from 3 categoires of Sport , Social, Home ; but he did not choose
    > anything from Movies and others.:
    >
    > Sport : edit (hyperlink) , remove (hyperlink)
    > Social : edit (hyperlink) , remove (hyperlink)
    > Home edit (hyperlink) , remove (hyperlink)
    >
    > The user should have a choice to go back to each category page and edit it
    > (he will see his previous choices in check boxes), or just by clicking a
    > link in the final summary page, to remove it. The categories and options
    > under each are read from database. User's choices are not commited to DB
    > until he reviews the summary and clicks confirm button at the last stage.
    >
    > Thank you in advance
    > Vajra
     
    , Oct 4, 2007
    #2
    1. Advertising

  3. Vajra

    Lew Guest

    "Vajra" wrote:
    >> 1. What is the best way to keep user's choices as he surfs and adds options
    >> under each category ? I thought of using session vars , in form of arrays,
    >> but I wonder if there is a better more efficient way ?


    wrote:

    Please do not top-post.

    > Well first you should not be using session variables. You can use
    > hidden fields avaialble in html [sic]


    Why not use session variables?

    > <input type="hidden" name="" value-""> , By using hidden fields you
    > will be able to get all the values in each page you just need to get
    > all this values and store it back to the returning page.


    At the cost of increased traffic between client and server, and concomitant
    security issues, which would not be a problem with session variables.

    > For the last page editing thing there can be 2 ways to do it
    >
    > 1) Create a link in the jsp page in such a way that all the options
    > which they have selected goes with it
    >
    > eg: Sports <a href="Test.jsp?
    > sports=abc,cde&social=xyz,wxy&option=edit&category=sports"> Edit </a>


    Generally you're better off following a Model-View-Controller pattern,
    submitting a POST to a controller servlet and having it guide the response
    with request parameters, rather than tangling navigation and view in this way.

    > 2) second option is whenever the link is clicked you call a
    > javascript function which will do a post submit of the page and will
    > set a value of some variable which will tell whether it was edit or
    > remove and which category
    >
    > Sports <a href="#" onClick="DoSomething('Edit','Sports')" > Edit </a>
    >
    > <script>


    Or just use an "Edit" or "Remove" submit button.

    While Javascript can sweeten the user experience, it is by no means required
    for this scenario.

    Use "submit" inputs rather than links. Don't hit JSPs with a GET or POST;
    JSPs are for view components. Hit the controller servlet. That servlet will
    dispatch a RequestDispatcher.forward() to bring up the correct JSP. Google
    "Model-View-Controller", Sun's "Model 2" architecture, and the "Front
    Controller Pattern".

    --
    Lew
     
    Lew, Oct 5, 2007
    #3
  4. Vajra

    Guest

    On Oct 4, 4:23 pm, Lew <> wrote:
    > "Vajra" wrote:
    > >> 1. What is the best way to keep user's choices as he surfs and adds options
    > >> under each category ? I thought of using session vars , in form of arrays,
    > >> but I wonder if there is a better more efficient way ?

    > wrote:
    >
    > Please do not top-post.
    >
    > > Well first you should not be using session variables. You can use
    > > hidden fields avaialble in html [sic]

    >
    > Why not use session variables?
    >
    > > <input type="hidden" name="" value-""> , By using hidden fields you
    > > will be able to get all the values in each page you just need to get
    > > all this values and store it back to the returning page.

    >
    > At the cost of increased traffic between client and server, and concomitant
    > security issues, which would not be a problem with session variables.
    >
    > > For the last page editing thing there can be 2 ways to do it

    >
    > > 1) Create a link in the jsp page in such a way that all the options
    > > which they have selected goes with it

    >
    > > eg: Sports <a href="Test.jsp?
    > > sports=abc,cde&social=xyz,wxy&option=edit&category=sports"> Edit </a>

    >
    > Generally you're better off following a Model-View-Controller pattern,
    > submitting a POST to a controller servlet and having it guide the response
    > with request parameters, rather than tangling navigation and view in this way.
    >
    > > 2) second option is whenever the link is clicked you call a
    > > javascript function which will do a post submit of the page and will
    > > set a value of some variable which will tell whether it was edit or
    > > remove and which category

    >
    > > Sports <a href="#" onClick="DoSomething('Edit','Sports')" > Edit </a>

    >
    > > <script>

    >
    > Or just use an "Edit" or "Remove" submit button.
    >
    > While Javascript can sweeten the user experience, it is by no means required
    > for this scenario.
    >
    > Use "submit" inputs rather than links. Don't hit JSPs with a GET or POST;
    > JSPs are for view components. Hit the controller servlet. That servlet will
    > dispatch a RequestDispatcher.forward() to bring up the correct JSP. Google
    > "Model-View-Controller", Sun's "Model 2" architecture, and the "Front
    > Controller Pattern".
    >
    > --
    > Lew


    Making session variables is a costly affair for the server. And you
    should avoid using that in all the conditions. If you'r server is
    having load balancing then it will be more costly for all the servers.
    Here the user is going to traverse from one page to another so he does
    not need to use session variables. Hidden varaibles can do the trick.
    I don't think this application is passing any valuable information
    like password in hidden variables which need to be secured.

    If you are not using session variables then you have to send the data
    by either GET or POST internally you can use MVC 2 to handle this
    things

    Raj
     
    , Oct 5, 2007
    #4
  5. Vajra

    Guest

    Thank you for the replies and explanations .... Appreciated.

    Vajra
     
    , Oct 7, 2007
    #5
  6. Vajra

    Lew Guest

    wrote:
    > Making session variables is a costly affair for the server. And you


    Making hidden variables is a costly affair for the connection.

    Which cost is worse, and how are you judging that?

    > should avoid using that in all the conditions.


    I don't think so.


    > If you'r server is having load balancing then it will be more costly for all the servers.


    But /too/ much more costly? Not likely.

    > Here the user is going to traverse from one page to another so he does
    > not need to use session variables. Hidden varaibles can do the trick.


    At a cost.

    > I don't think this application is passing any valuable information
    > like password in hidden variables which need to be secured.


    So?

    > If you are not using session variables then you have to send the data
    > by either GET or POST internally you can use MVC 2 to handle this
    > things


    What is "MVC 2"?

    There is nothing wrong with session variables, properly used. If something
    needs to persist between requests, one must use either a session, which
    collapses all session knowledge into a single "hidden variable", the session
    token, or hidden variables as you describe. You seem to have an unhealthy
    fear of session variables.

    One should avoid session state in a web application generally, but when one
    has such state, then session variables can often be a viable solution.
    (Session objects should implement Serializable, which imposes additional
    responsibility on the programmer, as Serializable exposes the implementation
    of a class. See Joshua Bloch's excellent book /Effective Java Programming/,
    which all of us should buy.)

    --
    Lew
     
    Lew, Oct 7, 2007
    #6
  7. On Oct 8, 2:25 am, Lew <> wrote:
    > wrote:


    [snip]

    > > If you are not using sessionvariablesthen you have to send the data
    > > by either GET or POST internally you can use MVC 2 to handle this
    > > things

    >
    > What is "MVC 2"?


    According to the JavaRanch FAQ's, nothing:
    http://faq.javaranch.com/java/Model1Model2MVC

    --
    Chris
     
    Chris ( Val ), Oct 8, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nicholas
    Replies:
    5
    Views:
    397
    Hans Kesting
    Apr 21, 2005
  2. Marc Rochkind
    Replies:
    17
    Views:
    6,072
    Dale King
    Jul 14, 2003
  3. john_c
    Replies:
    11
    Views:
    1,567
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Feb 22, 2007
  4. albert kao
    Replies:
    1
    Views:
    1,667
    Daniel Pitts
    Apr 7, 2010
  5. Brent
    Replies:
    0
    Views:
    130
    Brent
    Dec 26, 2003
Loading...

Share This Page