-2

I have the following class A JAVA FILE(data)

 package p1;

 class data
 {  
private String pro;
private String sta;

   public void set1(String a)
   {
   pro=a;
   }

   public void set2(String b)
   {
   sta=b;
   }

   }

A class file to retrive data from db A JAVA FILE(conb)

  package p1;
  import java.sql.*;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.util.*;
  import p1.*;
  public class conb 
  {
  public static List<data> datadb() throws SQLException
   {
  List<data> n1=new ArrayList<data>();
 Connection conn = null;
 Statement st = null;
       try
       {
           String userName = "frank";
           String password = "asdf";
           String url = "jdbc:mysql://localhost:8080/work";
           Class.forName ("com.mysql.jdbc.Driver").newInstance ();
           conn = DriverManager.getConnection (url, userName, password);

       }
       catch (Exception e)
       {
    e.printStackTrace();               

       }

        try {

            st = conn.createStatement();
            st.execute("select * from work1");
            ResultSet rs = st.getResultSet();
            while(rs.next())
                {
                    data d1=new data();
                    d1.set1(rs.getString("pron"));
                    d1.set2(rs.getString("sdata"));
                    n1.add(d1);
                }
            rs.close();
            }

        catch (SQLException e)
            {
            e.printStackTrace();

            }
       finally
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
               }
               catch (Exception e) 
       {
        e.printStackTrace(); 
       }
           }
       }
return n1;
   }
 }

This files are in WEB-INF/Classes/p1 And i want to display the data in a jsp file (using jsp as controller and view) I am using tomcat 6.0 in windows. I have written jsp page also for this

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <%@ import "java.util.*" %>
 <%@ import "p1.conb" %>
 <!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
 </head> 
 <body> 
 <table cellpadding="2" cellspacing="2" width="100%">

    <tr>
        <td>Name</td>
        <td>Start time</td>
        </tr>
 <tr>
 <%
 List<data> da1 = new List<data>();
 da1=p1.conb.datadb();
 %>
 <c:forEach items="${da1}" >        
 <td><c:out value="{$da1.pro}" /></td>
 <td><c:out value="{$da1.sta}" /></td>
 </tr>
 </c:forEach>
</table>


 <html>
 <head>

I am getting errors how can i retrive the data from database using only jsp. I dont want to use a servlet as a controller.

The errors are:

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:1‌​61) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340) 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)

theSound
  • 1
  • 2
  • 5
  • Please tell us which errors you get, post the Exception stacktrace! – home Oct 05 '11 at 19:26
  • What are the errors? Why not use MVC? Scriptlets are difficult to maintain – CoolBeans Oct 05 '11 at 19:30
  • errors are org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:161) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:340) 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) – theSound Oct 05 '11 at 19:33
  • Add the Caused By clause from the stack trace.. – CoolBeans Oct 05 '11 at 19:34
  • Try to change `pkg.conb.datadb()` to `p1.conb.datadb()`. – home Oct 05 '11 at 19:39
  • there are no errors in java program. my jsp cant retrieve the data – theSound Oct 05 '11 at 19:41
  • That part of the code is definitly wrong. The exception does not indicate that your result set is simply empty. How does the whole stacktrace look like? – home Oct 05 '11 at 19:46
  • 1
    Btw: the EL variables inside the `c:...` tags will not resolve to the variable defined in your scriptlet. – home Oct 05 '11 at 19:48

2 Answers2

1

I must admint that I do not feel comfortable with this answer, but if you simply want to get it running remove the <c:forEach [...] loop.

Replace this

<%
List<data> da1 = new List<data>();
da1=p1.conb.datadb();
%>
<c:forEach items="${da1}" >        
    <td><c:out value="{$da1.pro}" /></td>
    <td><c:out value="{$da1.sta}" /></td>
    </tr>
</c:forEach>

with something like this (you have to create the getter methods in your data class:

<%
List<data> da1 = new List<data>();
da1=p1.conb.datadb();

for (data da : da1) {
%>
    <tr>
    <td><%=da.getPro()%></td>
    <td><%=da.getSta()%></td>
    </tr>
<%
}
%>

Just for info - there is a huge number of issues with your code, to name a few:

  • scriptlets in JSP are a bad idea
  • classes do not conform to Java naming conventions at all
  • HTML tags are inconsistent, e.g. at the end of the document, not well formatted
  • database connection initialization on each call to JSP (this will not scale)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
home
  • 12,468
  • 5
  • 46
  • 54
0

I realize that I'm late to the party, but I've come across this type of problem many times as I've refactored JSPs to stop using scriptlets. In the intermediate stages, there are a lot of times where I have scriptlets that load data, but I want to use that data with some JSTL tags/EL expressions. This is my usual, temporary solution:

<%
List<data> da1 = new List<data>();
da1=p1.conb.datadb();

pageContext.setAttribute("da1", da1);
%>
<c:forEach items="${da1}" >        
    <td><c:out value="{$da1.pro}" /></td>
    <td><c:out value="{$da1.sta}" /></td>
    </tr>
</c:forEach>

You could just as easily add your data as an attribute in the request scope instead, but I start as local as possible and move to broader contexts as I need it.

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55