0
<TABLE>
    <TR>
        <TD>
            <% for(int i=0;i<categories.size();i++){ %>

                <button name="<%=categories.get(i)%>" type="button" onclick=<% session.setAttribute("category", categories.get(i));%> >
                    <%= categories.get(i)%>
                </button>
           <% } %>
       </TD>
    </TR>
    <TR>
        <TD>
            <% 
            if(session.getAttribute("category") != null){
                for(int i =0; i< pr.products.size();i++){
                    if(pr.products.get(i).category.equals(session.getAttribute("category"))){
                        out.print(pr.products.get(i).name);
                    }           
                }
            }%>
        </TD>
    </TR>
</TABLE>

I'm trying to show the products in a category when clicking a button but since the page is not refreshed nothing changes and I can't call sendRedirect when a button is pressed.

How do I get around this?

<button name="Phones" type="button" onclick="">
    Phones
</button>
JamesENL
  • 6,400
  • 6
  • 39
  • 64
Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43
  • Can you show some sample output of the HTML *after* the JSP has been parsed and sent to the client browser? You can see this parsed markup by right-clicking the page and choosing "View Page Source" (or equivalent). This will help determine if the produced markup is syntactically correct. – ajp15243 May 20 '14 at 04:18
  • I added the button's html code, they are only 2 (tablets and phones) – Mihai Bratulescu May 20 '14 at 04:21
  • Can you tell what you are trying to achieve? – mahesh May 20 '14 at 04:24
  • I want to set the category attribute and then show the items in that category based on the atribute whenever I click on a button representing a category name – Mihai Bratulescu May 20 '14 at 04:26
  • is it possible <% session.setAttribute("category", categories.get(i));%> ? you should call javascript function for onClick of a button – user3470953 May 20 '14 at 04:43

1 Answers1

0

You may use form & hidden tag instead, try this:

<TABLE>
    <TR>
        <TD>
            <% for(int i=0;i<categories.size();i++){ %>
                <form action="urlToUpdate"> <!-- use the controller to update session attribute -->
                  <input type="hidden" name="category" value="<%=categories.get(i)%>"/>
                  <button type="submit"><%=categories.get(i)%></button>
                </form>
           <% } %>
       </TD>
    </TR>
  • I noticed that the on onclick attribute is actually executing the code and not waiting for a click any way to set it up so that it executes when clicked? – Mihai Bratulescu May 20 '14 at 05:03
  • the best way is to avoid the java codes here . follow the MVC architecture instead http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Santhosh May 20 '14 at 05:18