0

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;
}
}
Community
  • 1
  • 1
theSound
  • 1
  • 2
  • 5

2 Answers2

4

The List interface does not have get1() and get2() methods. All it has is a get() method. Perhaps you intended to use that instead:

<td><%out.print(da1.get(1));%></td>
<td><%out.print(da1.get(2));%></td>

Note that the list index starts with 0, not 1. 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 a main() method which prints the results by System.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):

List<Work> works = workService.list();

for (Work work : works) {
    System.out.println(work.getPropertyNumber());
    System.out.println(work.getStartDate());
}

And here's how you would normally loop over it in a JSP

<c:forEach items="${works}" var="work">
    <td>${work.propertyNumber}</td>
    <td>${work.startDate}</td>
</c:forEach>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • the List class data have this get1() function and get2() function. i am providing the code for data.java – theSound Oct 07 '11 at 17:01
  • Yes, but you're calling them on the `List` interface, not on the `data` item the list holds. The `da1` is a `List`, not `data`. You would need for example `da1.get(0).get1()` to call `get1()` method on the 1st item of the `List`. I'd suggest to learn as well how to loop over a list in Java. The `while()` as you've there doesn't do that at all. – BalusC Oct 07 '11 at 17:02
  • I want to show all the data in my database. so i put while loop . i tried for(data da : da1) also but its showing da cannot be resolved to da1. – theSound Oct 07 '11 at 17:12
  • That can happen if you did not `import` the `data` class in the JSP. You would need to use the full qualified package name `pkg.data`. How long have you played with Java? I'd really suggest to forget JSP for a while and work through some basic oracle.com tutorials first. Most mistakes you made are already covered by the 1st chapter of the average Java tutorial. Don't run with JSP before you can walk with Java, you would otherwise end up in slowly creeping. – BalusC Oct 07 '11 at 17:14
  • I need to solve the problem . i have to use jsp both as controller and view. i am feeling helpless. suggest me some links and some solution too. i am new very new to java. – theSound Oct 07 '11 at 17:27
  • There are a lot of links in my answer, just follow them for more detailed explanation and examples (look at the blueish words and click them; to start, I think the "servlet" link is most helpful for you) and I've also posted an example how to loop over a list in both normal Java as in normal JSP. – BalusC Oct 07 '11 at 17:28
0

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.

<%out.print(da1.get(1));%>
<%out.print(da1.get(2));%>
Sid
  • 4,893
  • 14
  • 55
  • 110