求救!我做了一个用session对象来保存checkbox复选框的选中值,并在另一个页面中显示的程序,可报错了!请指点一下。
该程序共有两个文件 buy.jsp 和show.jsp
文件buy.jsp如下:
<head>
<title>
Doing Some Shoppings
</title>
</head>
<body>
<Script language= "java ">
void fun()
{
var len=document.shop.choice.length;
for(int i=0;i <len;i++)
{
document.shop.choice[i].checked=document.shop.all.checked;
}
}
</Script>
<form name= "shop " action= "show.jsp " method= "post ">
What do you want to buy: <input type= "checkbox " name= "choice " value= "basketball " checked> basketball
<input type= "checkbox " name= "choice " value= "football "> football
<input type= "checkbox " name= "choice " value= "baseball "> baseball
<input type= "checkbox " name= "choice " value= "all " onclick= "fun() "> all
<br> <br>
<input type= "submit " name= "go ">
<input type= "reset " name= "reset ">
</form>
</body>
</html>
文件show.jsp如下:
<html>
<head>
<title>
show the things
</title>
</head>
<body>
<%
for(int i=0;i <3;i++)
{
String names[i]=request.getParameterValues();
}
session.setAttribute( "first ",names[0]);
session.setAttribute( "second ",names[1]);
session.setAttribute( "third ",names[2]);
%>
<%=(String)session.getAttribute( "first ") %>
<%=(String)session.getAttribute( "first ") %>
<%=(String)session.getAttribute( "first ") %>
</body>
</html>
报错如下:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 8 in the jsp file: /checkbox/show.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\web\org\apache\jsp\checkbox\show_jsp.java:52: '] ' expected
String names[i]=request.getParameterValues();
^
1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
望指点一下。
--------------------------------------------
[解决办法]
String[] names = request.getParameterValues( "choice ");
以上代码中names的长度是由你复选的个数决定的,所以如果只选了basketball那么以下代码
session.setAttribute( "first ",names[0]);
session.setAttribute( "second ",names[1]);
session.setAttribute( "third ",names[2]);
中的names[1],names[2]是不存在的,运行时肯定会错
以下:
<%
String names[]=request.getParameterValues( "choice ");
String[] balls=new String[3];
for(int i = 0;i <3;i++){
balls[i] = new String();
}
for(int i = 0;i < names.length;i++){
balls[i] = names[i];
}
for(int i = 0;i <names.length;i++){
session.setAttribute( "first ",balls[0]);
session.setAttribute( "second ",balls[1]);
session.setAttribute( "third ",balls[2]);
}
%>
<%=(String)session.getAttribute( "first ") %>
<%=(String)session.getAttribute( "second ") %>
<%=(String)session.getAttribute( "third ") %>
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html