JSP页面读取数据库总是出不来,求大神指点
我是用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>