org.apache.jasper.JasperException:无法编译 JSP 类:
可能的重复:
如何在JSP/Servlet中上传文件?
我使用的是 tomcat 6.0 我有以下 jsp 代码
<%@ page import="java.util.*" %>
<!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
<html>
<head>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Start Date</td>
</tr>
<%
List<data> da1 = conb.dat();
while(da1 != null) {
%>
<tr>
<td><%out.print(da1.get1());%></td>
<td><%out.print(da1.get2());%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
,但收到此错误
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 68 in the jsp file: /trands.jsp
The method get1() is undefined for the type List<data>
65: <%
66: data da = new pkg.data();
67: List<data> da1 = conb.dat();
68: while(da1 != null) {
69: %>
70: <tr>
71: <td><%out.print(da1.get1());%></td>
An error occurred at line: 69 in the jsp file: /trands.jsp
The method get2() is undefined for the type List<data>
66: data da = new data();
67: List<data> da1 = conb.dat();
68: while(da1 != null) {
69: %>
70: <tr>
71: <td><%out.print(da1.get1());%></td>
72: <td><%out.print(da1.get2());%></td>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java: 92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
java 文件位于 WEB-INF/classes/pkg 文件夹中 为什么我会收到此错误。以及为什么它显示 get1() 未定义。我在 data.java 中有 get1() 函数。 connectdb() 是 jdbc java 文件)。我已经编译了所有 java 类来获取 .class 文件。这个 while 循环会显示我的数据库的所有详细信息
data.java 的代码
package pkg;
public class data
{
public String propn;
public String startd;
public data()
{
}
public void set1(String a)
{
propn=a;
}
public String get1()
{
return propn;
}
public void set2(String b)
{
startd=b;
}
public String get2()
{
return startd;
}
}
Possible Duplicate:
How to upload files in JSP/Servlet?
I am using tomcat 6.0
I have the following jsp code
<%@ page import="java.util.*" %>
<!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
<html>
<head>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Start Date</td>
</tr>
<%
List<data> da1 = conb.dat();
while(da1 != null) {
%>
<tr>
<td><%out.print(da1.get1());%></td>
<td><%out.print(da1.get2());%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
and i am getting this error
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 68 in the jsp file: /trands.jsp
The method get1() is undefined for the type List<data>
65: <%
66: data da = new pkg.data();
67: List<data> da1 = conb.dat();
68: while(da1 != null) {
69: %>
70: <tr>
71: <td><%out.print(da1.get1());%></td>
An error occurred at line: 69 in the jsp file: /trands.jsp
The method get2() is undefined for the type List<data>
66: data da = new data();
67: List<data> da1 = conb.dat();
68: while(da1 != null) {
69: %>
70: <tr>
71: <td><%out.print(da1.get1());%></td>
72: <td><%out.print(da1.get2());%></td>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java: 92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
java files are in WEB-INF/classes/pkg folder
why am i getting this error. and why its showing that get1() is undefined. i have get1() function in data.java. connectdb() is jdbc java file). I have compiled all the java class to get the .class files. and will this while loop show me all the details of my database
CODE FOR data.java
package pkg;
public class data
{
public String propn;
public String startd;
public data()
{
}
public void set1(String a)
{
propn=a;
}
public String get1()
{
return propn;
}
public void set2(String b)
{
startd=b;
}
public String get2()
{
return startd;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
List
接口 没有get1()
和get2()
方法。它所拥有的只是 a <代码>get()方法。也许您打算改用它:请注意,列表索引以
0
开头,而不是1
。也许您也想改变这一点。另请参阅:
不相关对于具体问题,我不知道你想做什么,但我只想提一下这段代码是一场灾难,并暴露了更多其他问题(首先,你认为用
while (da1 !=空)
?你想无限循环地展示它们吗?)。我强烈建议忘记这方面的 JSP,并将所有工作移至普通 Java 类,然后使用main()
方法将其作为普通 Java 应用程序进行测试,该方法通过打印结果>System.out.println()
。这使得开发、使用和测试变得更加容易。一旦一切正常,您就可以在 servlet 中导入并调用该类,最终让 JSP 显示结果。更新:
这是您通常希望循环列表的方式(我已经修复了可怕的命名约定基于最佳
href="https://stackoverflow.com/questions/7666544/use-jsp-as-both-view-and-controller">猜测,以便 通常会在 JSP 中对其进行循环
The
List
interface does not haveget1()
andget2()
methods. All it has is aget()
method. Perhaps you intended to use that instead:Note that the list index starts with
0
, not1
. Perhaps you'd like to change that as well.See also:
Unrelated to the concrete problem, I have no idea what you're trying to do, but I would only mention that this code is a disaster and exposes more other problems (to start, what did you think to achieve with
while (da1 != null)
? did you want to show them in an infinite loop?). I'd strongly recommend to forget JSP for this bit and move all the work to a normal Java class which you in turn test as normal Java application with amain()
method which prints the results bySystem.out.println()
. That's much easier developing, playing and testing. Once you got it all to work, then you can import and call that class in a servlet which ultimately lets the JSP display the results.Update:
Here's how you would normally like to loop over a list (I've fixed the terrible naming conventions for you based on best guesses so that the code is more self-documenting):
And here's how you would normally loop over it in a JSP
da1 变量的类型为 List。它不包含方法 get1() 和 get2()。您可能想要的是 da1.get(1)、da1.get(2) 等。
The da1 variable is of type List. It does not contain method get1() and get2(). What you probably want is da1.get(1), da1.get(2) etc.