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

qq_31045649 2015-09-09 03:37:06
我是用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>
...全文
103 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
qq_31045649 2015-09-09
现在能出来表格只是出不来数据
回复
qq_31045649 2015-09-09
所以就不要那几行了吗?还有什么错吗具体要怎么改啊~
回复
街头小贩 2015-09-09
servlet中的doGet对应着servlet绑定路径上的get请求.例:/A跟servletA绑定,当你在地址上输入/A时,哪么此时的响应内容是由doGet输出的(返回的)
回复
街头小贩 2015-09-09
同学你在干么?

  ProductListServlet  pls = new ProductListServlet();
    pls.doGet(request,response);
    request.getAttribute("productlist");
不懂要学,不要乱写.每一个jsp文件都会被容器自动转成servlet.servlet由容器实例化,不用你自已new.不懂java不要紧,可以用jstl标签来写。不用写任何代码
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-09 03:37
社区公告
暂无公告