0

I have a JSP that gets a ResultSet object from a database, then it shows the values of the database in a table with checkboxes ready for selection, the only thing im having problems is getting those checked box values so that I can send them to my servlet then to another page showing a table of the selected items.

I've check everything that I can and I still cant find the answer.

Here's what i got: JSP for viewing data:

<%
                ResultSet results = (ResultSet)request.getAttribute("results");
                int countme=0;
                int numb=1;

        while (results.next()) {
                        ++countme;
                        numb=countme;

                    %>

        <tr>
                            <td><input type ="checkbox" id="cb_<%=(numb)%>"
                                       class="cbgroup"
                                       value="<%out.println(results.getString("ANI_TITLE"));%>"
                                       name="cbg" 
                                       />
                            </td>
            <td><%=results.getString("ANI_TITLE") %></td>
            <td><%=results.getString("ANI_STUDIO") %></td>
                            <td><%=results.getString("ANI_EPI") %></td>
        </tr>   
    <%  }

    %>

Then, once checked, you click the button that goes to this Servlet part:

else if (btn.equals("Continue")){
        token="delcont";
        String[] vals1=request.getParameterValues("cbg");
        }

        request.setAttribute("vals1", vals1);
        request.setAttribute("token", token);

        RequestDispatcher dispatch = request.getRequestDispatcher("deletechecked.jsp");
        dispatch.forward(request, response);


            }

and then the servlet sends it to this JSP for viewing the checked values in a table:

<h2>Are you sure to delete these?</h2>
    <table border="1" align="center">
    <tr>
        <th>Anime Title</th>
        <%--<th>Anime Studio</th>
                    <th>No. of Episodes</th>--%>
    </tr>
    <%
                String[] values = request.getParameterValues("cbg");
                String holder="";
                for(int i=0;i < values.length;i++){


                %>
                <tr><td><%=values%></td></tr>
                <% 
                    }
            %>

This code outputs a Hashcode of the array. We dont want that.

Hope you can help me. Thanks.

UPDATE: I got it working, but it prints the hashcode instead of the string. *Updated the Code above. It prints [Ljava.lang.String;@29cfc19f] as the array element.

Xenos29
  • 17
  • 8
  • Here, we have no idea how you send your request for the client to the serveltsm where the exception occurs exactly, .... did you check the request content ? – AxelH Dec 02 '16 at 13:12
  • @AlexH i send the request via `
    ` and with this button redirecting it to the servlet `` The request content should be in vals1, but i checked it and it has "null" in it even when you check something.
    – Xenos29 Dec 02 '16 at 13:18
  • You should know that in HTML, the value of a checkbox is only present if it is checked, if not there is nothing. Check this http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked – AxelH Dec 02 '16 at 13:20
  • Well, I do know that. But it still doesn't get the value of the checked box. There must be a problem in `"name="cbg" />` that doesnt allow it to get the value from the table record. Or in the `String[] vals1=request.getParameterValues("cbg");` part, where it places the values to an array. – Xenos29 Dec 02 '16 at 13:25
  • Update your question, add the resulting from the client. Check if the values are correct.
    – AxelH Dec 02 '16 at 13:29
  • I haven't coded the table yet, but it show be something like this `<% String[] values; values=request.getParameterValues("vals1"); for(int i=0;i < values.length;i++){ %> <%=values%> <% } %>` Also,now it produces a [Ljava.lang.String;@57eec887 when i simply print out what vals1 after the passed value goes to the servlet by changing this: `value="<%=results.getString("ANI_TITLE")%>"` in to a out.println – Xenos29 Dec 02 '16 at 13:32
  • _I haven't coded the table yet_ You used a table to put your database result ... you need to debug it. But you are not trying enough so I will let you find the solution. `NullPointerException` are verbose. Debug this and you will find your problem. – AxelH Dec 02 '16 at 13:37
  • @AxelH Update, I got the values from the servlet after doing what I said in the last comment. BUT im still not able to get those values from the servlet to the View JSP, I think I need to do something about the `request.setAttributes` parts. – Xenos29 Dec 02 '16 at 13:49
  • You should use [jstl](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1) instead, much more readable. – AxelH Dec 02 '16 at 13:59

2 Answers2

1

Problem is you have set attribute as:

request.setAttribute("vals1", vals1);

But you are trying to get using getParameterValues() and not getAttribute():

values=request.getParameterValues("vals1");

Use instead:

values=(String[])request.getAttribute("vals1");

OR simply:

values=request.getParameterValues("cbg");

You are simply printing array and not string:

<tr><td><%=values%></td></tr>

To print each selected values use index:

<tr><td><%=values[i]%></td></tr> <!-- note `[i]` -->
0

Probably, you've got NullPointerException because when checkbox checked it's value is "on" (not a true) and when it's not the value is null (not a false)

first checkbox is not checked, but last checked same here

Community
  • 1
  • 1