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>
...全文
158 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
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标签来写。不用写任何代码

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧