简体   繁体   English

Java JSP servlet 看不到 JSP 中的 DB

[英]Java JSP servlet can't see DB in JSP

I begun to learn datebase and servlet, JSP. And I don't know some fails.我开始学习datebase和servlet,JSP。不知道有些失败。 I can't see my db in JSP.我在 JSP 中看不到我的数据库。

Java code: Java 代码:

try {

        Class.forName("org.postgresql.Driver");
        connection = DriverManager.getConnection(url, login, password);
        String sql = "select * fiz_phone";
        Statement s = connection.createStatement();
        s.executeQuery(sql);
        rs = s.getResultSet();
        while (rs.next()) {
            dataList.add(rs.getInt("id"));
            dataList.add(rs.getString("name"));
            dataList.add(rs.getString("adress"));
            dataList.add(rs.getString("phone"));
            dataList.add(rs.getString("phone_adress"));
            dataList.add(rs.getInt("cost"));
            dataList.add(rs.getString("exempt_type"));
            dataList.add(rs.getInt("exempt"));
            dataList.add(rs.getString("date_claim"));
            dataList.add(rs.getInt("number_claim"));
            dataList.add(rs.getString("inspektor"));
            dataList.add(rs.getString("date_repair"));
            dataList.add(rs.getInt("phone_cost"));
            dataList.add(rs.getString("call"));
        }
        rs.close();
        s.close();
    } catch (Exception e) {

        System.out.println("Exception is ;" + e);

    }

    request.setAttribute("data", dataList);

    RequestDispatcher dispatcher = request.getRequestDispatcher(page);

    if (dispatcher != null) {

        dispatcher.forward(request, response);

    }

HTML: HTML:

   <body>
    <table border="1">
        <tr>
            <td ><b>ID</b></td>
            <td ><b>Name</b></td>
            <td ><b>Adress</b></td>
            <td ><b>Phone</b></td>
            <td ><b>Phone adress</b></td>
            <td ><b>Cost</b></td>
            <td ><b>Exempt type</b></td>
            <td ><b>Exempt</b></td>
            <td ><b>Date claim</b></td>
            <td ><b>Number of claim</b></td>
            <td ><b>Inspektor</b></td>
            <td ><b>Date repair</b></td>
            <td ><b>Phone cost</b></td>
            <td ><b>Call</b></td>
    
    
        </tr>
        <%ArrayList<String> f = (ArrayList<String>) request
        .getAttribute("data");
        Iterator<String> itr = f.iterator();
            while (itr.hasNext()) {%>
    
        <tr id="tab">
    
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
            <td ><%=itr.next()%></td>
        </tr>
        <%}%>
    </table>
    </body>

and servlet和小服务程序

<servlet>
        <servlet-name>DataServlet</servlet-name>
        <servlet-class>work_project.Data</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DataServlet</servlet-name>
        <url-pattern>/DataServlet</url-pattern>
    </servlet-mapping>

When I start this program, it give me this exception当我启动这个程序时,它给了我这个例外

org.apache.jasper.JasperException: An exception occurred processing JSP page /DataPage.jsp at line 35

32:     </tr>
33:     <%Iterator itr;%>
34:     <% List data= (List)request.getAttribute("data");
35:         for (itr=data.iterator(); itr.hasNext(); ) {
36:     %>
37:     <tr>
38:         <td ><%=itr.next()%></td>

I ran an example of your code.我运行了您的代码示例。 It worked.有效。 The only difference was that I inserted some code into the data list, instead of accessing a database.唯一的区别是我在数据列表中插入了一些代码,而不是访问数据库。 Even if the result set was empty, you should not be getting a null pointer exception.即使结果集为空,也不应出现 null 指针异常。

Since you are getting a null pointer exception when you access the data list, it seems to indicate that the original list in the servlet was null. What is the code for instantiating the dataList in the servlet?由于访问数据列表时出现null指针异常,这似乎表明servlet中的原始列表为null。请问servlet中实例化dataList的代码是什么? Do you have a statement somewhere like你有没有像这样的声明

List<String> dataList = new ArrayList<String>();

Try debugging your code.尝试调试您的代码。 Set a breakpoint in the servlet on the setAtribute statement.在 servlet 的 setAtribute 语句中设置断点。 See if dataList is null.查看dataList是否为null。

Also check that the loop is executing.还要检查循环是否正在执行。 Be sure the result set is not empty.确保结果集不为空。

The code should be a valid SQL query by querying a database with JDBC statement.通过使用 JDBC 语句查询数据库,该代码应该是一个有效的 SQL 查询。

String sql = "select * from fiz_phone";

It might be a typo but you ignored it in the code for the catch block这可能是一个拼写错误,但您在 catch 块的代码中忽略了它

} catch (Exception e) {
    //TODO you should handle the exception here
    System.out.println("Exception is ;" + e);

}

Also since you are learning, you better return an object for each record from the resultset.此外,由于您正在学习,因此最好为结果集中的每条记录返回 object。

while (rs.next()) {
    MyObject obj = new MyObject();
    obj.setId(rs.getInt("id"));
    obj.setName(rs.getString("name"));
    ...

    dataList.add(obj);
}

Also do it in iterator也在迭代器中做

<%ArrayList<MyObject> f = (ArrayList<MyOobject>) request.getAttribute("data");
Iterator<MyObject> itr = f.iterator();
while (itr.hasNext()) {
  MyObject obj = itr.next();
  pageContext.setAttribute("row", obj);%>

<tr id="tab">
    <td >${row.id}</td>
    <td >$(row.name}</td>
    ...
</tr>
<%} pageContext.removeAttribute("row");%>

You should make MyObject a java bean (ie properties should have getters and setters).您应该使MyObject成为一个 java bean(即属性应该有 getter 和 setter)。 Latter you may try to remove scriptlets at all by using JSTL tag library and forEach tag.稍后您可能会尝试使用 JSTL 标记库和forEach标记完全删除 scriptlet。

Building on the answer of Roman C, the scriptlet scope is not the same as the expression language scope. If you use a jstl core template for the loop, then it should see row.基于 Roman C 的答案,scriptlet scope 与表达式语言 scope 不同。如果您对循环使用 jstl 核心模板,那么它应该看到行。

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  <c:forEach var="row" items="${data}">
    <tr id="tab">
        <td >${row.id}</td>
        <td >${row.name}</td>
    </tr>
  </c:forEach>

I do not think this will resolve the original problem.我不认为这会解决原来的问题。 data is probably still null.数据可能还是null。

Have you tried using the debugger and testing that dataList is not null when it is added to the request with setAttribute?您是否尝试过使用调试器并测试 dataList 在使用 setAttribute 添加到请求时不是 null?

Responding to comment about debugger:回应关于调试器的评论:

You need to step one more time, so that rows is initialized.您需要再执行一次,以便初始化行。 Try the same thing in the servlet for the setAttribute statement, to see if dataList is null.在 servlet 中对 setAttribute 语句尝试相同的操作,以查看 dataList 是否为 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM