This discussion is archived
1 Reply Latest reply: Aug 12, 2013 10:11 PM by S.K.Saha RSS

Get multiple textbox value in CheckBox values attribute in jsp and fetch the checkbox value in servlet

S.K.Saha Newbie
Currently Being Moderated

Hi,

 

i have written this in jsp

---------------------------------------------

 

 

<%@page import="com.sun.org.apache.xerces.internal.impl.xs.identity.ValueStore"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

<script type="text/javascript">

function getvalue(){

  var elems = document.getElementsByTagName('input');

  var values = [];

  for(var i=0; i< elems.length; i++){

  if (elems[i].type == "text"){

  values.push(elems[i].value);

  }

  }

  for(var i=0; i< values.length; i++){

  alert(values[i]);

  }

  request.setAttribute("res",values);

}

</script>

</head>

<body>

<form action="checkbox">

<table>

<tr>

<td>First Name:<input type="text" name="first"  id="text1"/></td>

<td>Second Name:<input type="text" name="second"  id="text2"/></td>

<td>check both<input type="checkbox" id="chk" name="allValue" value="<%=request.getAttribute("res")%>" onclick="getvalue()"/>

</td>

</tr>

<tr><td><input type="submit" value="OK" ></td>

</tr>

</table>

</form>

</body>

</html>

 

 

And Servlet Code

--------------------------

 

package com.test;

 

 

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

 

 

public class TestServlet extends HttpServlet {

 

 

  protected void service(HttpServletRequest req,HttpServletResponse res)

  {

  String[] str=req.getParameterValues("allValue");

  for(int i=0;i<str.length;i++)

  {

  System.out.println("checkbox:"+str[i]);

  }

 

  }

}

 

----------------------------------

 

here i am getting null value in servlet even though i have set the all textbox values in request in javascript function as i have written in above code

 

 

please suggest me if any solution is there

 

Thanks,

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points