代码
package com.MyUser.controller;
import java.io.IOException;
import java.util.List;
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.MyUser.domain.MyUser;
/**
* Servlet implementation class MyUserServlet
*/
@WebServlet("/MyUserServlet")
public class MyUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
MyUser myuserModel = new MyUser();
List<MyUser> list = myuserModel.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/jsp/list.jsp").forward(request, response);
}catch(Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.MyUser.domain;
import java.util.List;
public class MyUser {
private Integer gname;
private String price;
private String number;
private Integer gdate;
public Integer getGname() {
return gname;
}
public void setGname(Integer gname) {
this.gname = gname;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Integer getGdate() {
return gdate;
}
public void setGdate(Integer gdate) {
this.gdate = gdate;
}
public List<MyUser> findAll() {
// TODO Auto-generated method stub
return null;
}
}
package com.MyUser.model;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.MyUser.Utils.Utils;
import com.MyUser.domain.MyUser;
public class myuserModel {
public List<MyUser> findAll() throws SQLException{
QueryRunner queryRunery = new QueryRunner(Utils.getDataSource());
List<MyUser> list = queryRunery.query("select * from student",new BeanListHandler<MyUser>(MyUser.class));
return list;
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>商品信息显示页面</h1>
<table border="1" width="600">
<tr>
<td>商品名称</td>
<td>商品价钱</td>
<td>商品总数量</td>
<td>商品上架时间</td>
</tr>
<c:forEach var="myuser" items="${ list }">
<tr>
<td>${ myuser.gname }</td>
<td>${ myuser.price }</td>
<td>${ myuser.number }</td>
<td>${ myuser. gdate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>