2013-08-28 2 views
4

초보자입니다. 한 JSP 페이지에서 다른 JSP 페이지로 배열 값을 전달하려고합니다. 페이지 가져 오기 데이터가체크 박스 값의 배열을 전달하는 방법 한 JSP 페이지에서 다른 JSP 페이지로

<% 
    ResultSet rs=s.notapprovedqns(); 
%> 
<%     
    while(rs.next()) 
    { %> 
     <tr><td><input name="qns[]" type="checkbox" value="<% out.println(rs.getInt("question_id")); %>" /></td><td><center><%=rs.getString("question_id") %></center></td><td><%=rs.getString("question") %></td></td></tr> 
    <% 
     } 
     %> 

어떻게하면 다른 페이지에서 체크 박스 값을받을 수 있습니까? 나는 다음과 같은 코드를 시도하지만 제대로

String[] h=null; 
h=request.getParameterValues("qns[]"); 

작동하지 않습니다하지만 그것

[Ljava.lang.String;@a0a595 

누군가가 나에게이 문제를 해결하기 위해 도와주세요 값을 전달합니다.

h=request.getParameterValues("qns[]"); 
String item = h[0] 

또는 전체 배열을 반복하는 루프를 사용하면 다음과 같은 인덱스를 사용하여 요소를 얻을 필요가 있으므로

+0

'h'가'문자열 []'이다. 요소를 반복합니다. Object # toString()을 보았습니다. –

답변

6

당신은 그것을 사용할 수 말해서 간단하게 유지할 수 있습니다. process.jsp

에서 양식

<form method="post" action="process.jsp"> 
    <input type="checkbox" name="list" value="value1"> 
    <input type="checkbox" name="list" value="value2"> 
    <input type="checkbox" name="list" value="value3"> 
</form> 

에서

String[] ids=request.getParameterValues("list"); 
    // this will get array of values of all checked checkboxes 
    for(String id:ids){ 
    // do something with id, this is checkbox value 
    } 
+0

이것은 100 % 작동 ... – Mazhar

0

당신은 배열을 얻고있다.

1
for(int count=0; count<h.length; count++){ 
    // DO SOME OPERATION on h[count]; 
} 

또한, 단지 추천, qns[] 같은 변수 이름을하지 마십시오, 당신은 항상 다음과 같이 selectedItems

0

당신은 모두 StringBuilder()를 사용할 수있다, 나는이 작품을 희망 :

ResultSet rs=s.notapprovedqns(); 
StringBuilder lstquestion = new StringBuilder(); 
while(rs.next()) { 
    String question_id = rs.getString("question_id"); 
    String question = rs.getString("question"); 
    lstquestion.append('<tr><td><input name="qns[]" type="checkbox" value='+question_id+' /></td><td><center>'+question_id+'</center></td><td>'+question+'</td></td></tr>') 

} 
관련 문제