使用jsp作为视图和控制器

发布于 2024-12-08 12:23:20 字数 3225 浏览 0 评论 0 原文

我有以下课程 JAVA 文件(数据)

 package p1;

 class data
 {  
private String pro;
private String sta;

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

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

   }

用于从数据库检索数据的类文件 JAVA 文件(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;
   }
 }

该文件位于 WEB-INF/Classes/p1 中 我想在jsp文件中显示数据(使用jsp作为控制器和视图) 我在windows下使用的是tomcat 6.0。 我也为此编写了 jsp 页面,

 <%@ 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>

但出现错误 我如何仅使用jsp从数据库检索数据。 我不想使用 servlet 作为控制器。

错误是:

org.apache.jasper.JasperException:org.apache.jasper.JasperException:无法加载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.爪哇:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始 2024-12-15 12:23:20

我必须承认,我对这个答案感到不舒服,但如果您只是想让它运行,请删除 循环。

将其替换

<%
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>

为类似的内容(您必须在 data 类中创建 getter 方法:

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

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

仅供参考 - 您的代码存在大量问题,仅举几例:

  • JSP 中的 scriptlet一个坏主意,
  • 类不符合 Java 命名约定,所有
  • HTML 标记不一致,例如在文档末尾,
  • 每次调用 JSP 时数据库连接初始化格式不正确(这不会扩展)

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)
慈悲佛祖 2024-12-15 12:23:20

我意识到我迟到了,但是当我重构 JSP 以停止使用 scriptlet 时,我已经多次遇到过此类问题。在中间阶段,很多时候我都有加载数据的 scriptlet,但我想将该数据与一些 JSTL 标签/EL 表达式一起使用。这是我通常的临时解决方案:

<%
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>

您可以轻松地将数据添加为请求范围中的属性,但我会尽可能从本地开始,然后根据需要转移到更广泛的上下文。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文