JSP页面读取数据库总是出不来,求大神指点

qq_31045649 qq_31045649 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我是用servlet+JSP读
然后,这是servlet的代码:
package jspservlet.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jspservlet.dao.ProductDAO;
import jspservlet.dao.UserDAO;
import jspservlet.dao.impl.ProductDAOImpl;
import jspservlet.dao.impl.UserDAOImpl;
import jspservlet.vo.Product;
import jspservlet.vo.User;

public class ProductListServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

List<Product> list = new ArrayList<Product>();
ProductDAO dao=new ProductDAOImpl();
String sql = "select * from productinfo";
response.setContentType("text/html");

try {
list = dao.getproductList(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

request.setAttribute("productlist", list);
request.getRequestDispatcher("ProductList.jsp").forward(request, response);



}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{




}

}







这是JSP代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<% String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<%@ page import="jspservlet.servlet.*"%>


<form method="post" action="./list">
<%
ProductListServlet pls = new ProductListServlet();
pls.doGet(request,response);
request.getAttribute("productlist");

%>
<table border="1"cellpadding="0" cellspacing="0"width="80%" align="center">

<tr>

<th>ID</th>

<th>product name</th>

<th>product value</th>

<th>product number</th>

</tr>


<c:forEach items="${requestScope.productlist}" var="product">
<tr>

<td>${ptoduct.ID }</td>
<td>${product.productname }</td>
<td>${product.productvalue }</td>
<td>${product.productnumber }</td>

</tr>
</c:forEach>

</table>
</form>

</body>
...全文
评论 4
收藏 已收藏
分享
返回顶部
qq_31045649
Web 开发 81,107 成员 341,720 内容