开发者

Capture multiple check box selection JSP Parameters

开发者 https://www.devze.com 2022-12-22 11:06 出处:网络
I found this post that shows how to pass multiple check box selections to another JSP page, but it\'s not working for me.When I try to get the selected values I get:

I found this post that shows how to pass multiple check box selections to another JSP page, but it's not working for me. When I try to get the selected values I get:

checked boxes: [Ljava.lang.String;@3f3fbd

Here are my two pages (be gentle, this is my first attempt at JSP!)

createSHAREfile.jsp basically runs a query to find all the terms that have not been processed and show each term with a check box next to it:

  <title>Create SHARE Files</title>
</head>
<body>
  <jsp:include page="../menu/header.jsp" flush="false" />
  <form name='SelectSHARETerms' method='post' action="SHAREProcessing.jsp">
    <fieldset><legend>Select Terms to Process for SHARE</legend>
    <table align='left'>
    <% String termDetail = "", currDate = "";
       currentDateTime datetime = new currentDateTime();
       datetime.setCurrDate();
       currDate = datetime.getCurrDate();
       java.sql.Date todayDate = java.sql.Date.valueOf(currDate);
       Terms terms = new Terms();
       ArrayList<Terms.termsTable> termsObjList = new ArrayList<Terms.termsTable>();
       terms.setTermsSql("Select * from Terms where TermDate <= '" + currDate + "' AND VoucherProcessDate Is Null");
       boolean indicator = terms.setListOfTerms();
       if (indicator == true) {
         int size = terms.getListSize();
         termsObjList = terms.getTermsList();
         for (int i=0; i<size; ++i) {
           Terms.termsTable eachTerm = (Terms.termsTable)termsObjList.get(i);
           java.sql.Date termDate = eachTerm.TermDate;
     %>         
      <tr><td><input type=checkbox name=SelectedTermDate id='SelectedTermDate<%=i%>' value="<%=i%>"><%=termDate %></td></tr>
    <%
         }
       }
     %>
      <tr><td align='center'><input type='submit' value='Submit'></input></td></tr>
    </table>
    </fieldset>
    </form>
  </body>
</html>

When the submit button is pressed I call SHAREProcessing.jsp. Right now all i'm trying to do on this page is show which termdates the user has selected so I can use them as parameters to a Java Class that will create the files for the selected terms:

  <title>SHARE Processing</title>
</head>
<body>
<jsp:include page="../menu/header.jsp" flush="false" />
<table width='50%' align='center' border='1'>
 <% String[] SelectedValues = request.getParameterValues("SelectedTermDate");
    System.out.println("checked boxes: " + SelectedValues);
  %>
</body>
</html>

Here's where I'm trying to use the code shown in the other post but it's not working :(

Thanks for 开发者_如何转开发any help! Leslie


You're trying to print the whole string array with System.out.println, and so you get that. It's probably working fine.

Try this:

System.out.println("checked boxes:");
for (int i = 0; i < SelectedValues.length; ++i)
  System.out.println("  " + SelectedValues[i]);

Also, I beg you: in your spare time, find out about a modern web framework (there are zillions for Java) and strive to escape from the painful world of coding Java inside JSP files.


You're just facing the default value of Object#toString().

Either just loop over it and print each item, or use Arrays#toString(). Here's an SSCCE:

package com.stackoverflow.q2426380;

import java.util.Arrays;

public class Test {

    public static void main(String... args) {
        String[] array = {"foo", "bar" , "waa"};
        System.out.println(array); // [Ljava.lang.String;@addbf1

        String arrayAsString = Arrays.toString(array);
        System.out.println(arrayAsString); // [foo, bar, waa]
    }

}

That said, this problem has actually nothing to do with JSP. It's just a view technology. The problem is rather in the basic Java code --which you wrote at the wrong place, in a JSP file instead of a Java class. I strongly agree with the comments that writing raw Java code in JSP files is a bad practice. Start learning Servlets.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号