【问题标题】:Printing DB2 Query Results to JSP from Java Servlet [duplicate]从 Java Servlet 将 DB2 查询结果打印到 JSP [重复]
【发布时间】:2017-07-19 11:14:02
【问题描述】:

我的动态 Web 项目中的 response.jsp 没有显示来自数据库的结果。下面是我的代码部分,连接建立没有任何问题

OrderController.java

package com.whs.reporting.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.waitrose.reporting.dao.OrderDao;

/**
 * Servlet implementation class OrderC
 */
@WebServlet(name="OrderServlet",urlPatterns={"/OrderController"})
public class OrderController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        OrderDao dao = new OrderDao();
        try {           
            request.setAttribute("orders",dao.getallorders());          
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        RequestDispatcher view = request.getRequestDispatcher("Response.jsp");
        view.forward(request, response);        
    }
}

Response.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Show Order Result</title>
</head>
<body>
    <table border=1>
        <thead>
            <tr>
                <th>Order Number</th>
                <th>Service Type</th>                
                <th>Delivery Date</th> 
                <th>Branch Number</th>               
                <th>Total</th>               
            </tr>
        </thead>
        <tbody>
        <c:forEach items="${orders}" var="order">
                <tr>
                    <td>${order.ordernumber}</td>
                    <td>${order.service}</td>
                    <td>${order.deliverydate}</td>
                    <td>${order.branch}</td>
                    <td>${order.total}</td>                    
                </tr>
          </c:forEach>
        </tbody>
    </table>
</body>
</html>

谁能让我知道我在这里缺少什么以及为什么 jsp 不显示数据库结果?

【问题讨论】:

  • 您应该只发布您遇到问题的最相关的代码,除非有人明确要求查看更多代码。
  • 您是否检查过您的代码没有产生任何异常?
  • 谢谢,我用servelet和response.jsp更新了问题,生成的代码没有任何异常
  • @ChimbuDurai 以下答案是否解决了您的问题?
  • 我添加了标签,但收到错误“javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag” 我还添加了 jstl.jar网络应用程序库。但仍然无法正常工作

标签: jsp jstl


【解决方案1】:

在您的response.jsp 中包含以下链接

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/xml" %>

因为使用的是标签库,所以需要在使用jstl标签的jsp中提供。

<%@ page language="java" contentType="text/html; charset=EUC-KR" 
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/xml" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Show Order Result</title>
</head>
<body>
<table border=1>
    <thead>
        <tr>
            <th>Order Number</th>
            <th>Service Type</th>                
            <th>Delivery Date</th> 
            <th>Branch Number</th>               
            <th>Total</th>               
        </tr>
    </thead>
    <tbody>
    <c:forEach items="${orders}" var="order">
            <tr>
                <td>${order.ordernumber}</td>
                <td>${order.service}</td>
                <td>${order.deliverydate}</td>
                <td>${order.branch}</td>
                <td>${order.total}</td>                    
            </tr>
      </c:forEach>
    </tbody>
</table>

this can be help ful for reference

【讨论】:

  • 我添加了标签,但收到错误“javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag” 我还添加了 jstl.jar网络应用程序库。但仍然无法正常工作 -
  • @ChimbuDurai 尝试在您的 tomcat 中清理和重新部署,或尝试以下method 和此method
  • 添加了 jstl-1.2.jar,它现在可以工作了。非常感谢!!
  • @ChimbuDurai 太棒了,然后使用绿色勾号支持这个答案并接受它作为工作答案:)。很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2018-11-03
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
相关资源
最近更新 更多