tags:

views:

39

answers:

1

I'm trying to store a drop-down's setting that's controlling how many items to display in a roster. How do I go about doing this? The dropdown is using onChange to sort the results of the roster.

+1  A: 

I don't do Velocity, so here's a JSP/Servlet-targeted answer. I suppose you're using Servlets as well, as Velocity is actually a templating engine and does nothing to control/preprocess/postprocess requests.

So you want to retain certain data in the subsequent requests? There are basically two ways to achieve this.

  1. Retain the value for the subsequent request in a hidden input element. E.g.

    <form action="servlet" method="post">
        <select name="itemcount" onchange="submit()">
            <option>1</option><option>2</option><option>3</option>
        </select>
    </form>
    

    and then in the form of the next request:

    <form action="servlet" method="post">
        <select name="sortfield" onchange="submit()">
            <option>col1</option><option>col2</option><option>col3</option>
        </select>
        <input type="hidden" name="itemcount" value="${param.itemcount}">
    </form>
    

    The ${param.itemcount} basically returns request.getParameter("itemcount"). When stored in a hidden input element you see nothing, but it will be as well available by request.getParameter("itemcount") in the next request.

  2. Store the value in session. E.g. inside the servlet:

    Integer itemcount = Integer.valueOf(request.getParameter("itemcount"));
    request.getSession().setAttribute("itemcount", itemcount);
    

    so that you can access it in any servlet running in the same session as follows whenever needed:

    Integer itemcount = (Integer) request.getSession().getAttribute("itemcount");
    

    But this has a major caveat: this may cause "wtf?" experiences when the user has multiple windows open inside the same session and selects a different itemcount in the both windows. The last selected value in window A would be reflected into window B!

I think it's obvious that you should keep request scoped data in the request scope, so way 1 is more preferred. Use the session only for session scoped data.

BalusC
BalusC, thanks for answer man. I tried to implement this, and it yieled no results. I'm fairly new to this. I took over a site, and it's in velocity etc. Do you think I could use $request.session.setAttributeand then request the session through getAttribute? Thanks again.
Reden