当我执行此代码时,它会抛出NullPointerException
。我想要如何在JSP中存储多个复选框的值。
我在此代码中选择3个复选框的值:
<%@ page import="java.sql.*,javax.sql.*" %>
<%@ include file="DBCON.jsp"%>
<%
String Branch=request.getParameter("Branch");
String Yr=request.getParameter("Yr");
String Sem=request.getParameter("Sem");
String Sec=request.getParameter("Sec");
String sub=request.getParameter("sub");
String date=request.getParameter("date");
String to_no_cls=request.getParameter("to_no_cls");
StringBuffer sql=new StringBuffer();
String[] ht_no=request.getParameterValues("ht_no");
for(int j=0;j<=ht_no.length;j++)
{
System.out.println(ht_no[j]);
}
for(int i=0;i<=ht_no.length;i++)
{
PreparedStatement pst=con.prepareStatement("insert into attd_entry values(?,?,?,?,?,?,?,?)");
pst.setString(1,Branch);
pst.setString(2,Yr);
pst.setString(3,Sem);
pst.setString(4,Sec);
pst.setString(5,sub);
pst.setString(6,date);
pst.setString(7,to_no_cls);
pst.setString(8,ht_no[i]);
pst.executeUpdate();
}
response.sendRedirect("attdentry.jsp");
%>
答案 0 :(得分:0)
如果未选中,则不会传递复选框。如果选中它,默认情况下将以“on”值传递。如果在提交表单时未检查request.getParameter(“checkbox1”)将返回null。否则,如果选中它将返回“开启”。只需检查有问题的参数
String checkBox1;
variable = request.getParameter("checkBox1");
if(variable == null)
{
//handle unchecked case
checkBox1 = "0";
}
else
{
//handle checked case
checkBox1 = "1";
}
insert(checkbox, test, test, test);