Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

java - use jsp as both view and controller

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...