jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.model.Home" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="https://bbs.csdn.net/topics/">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="https://bbs.csdn.net/topics/styles.css">
-->
</head>
<body>
<h3>宿舍管理系統</h3>
<a href="https://bbs.csdn.net/topics/insert.jsp">添加新宿舍</a>   <a href="https://bbs.csdn.net/topics/delete.jsp">洗掉宿舍</a>
<table border="1px">
<tr>
<th>宿舍門號</th>
<th>宿舍所在校區</th>
<th>宿舍詳細地址</th>
<th>宿舍聯系電話</th>
<th>宿舍人數</th>
<th>宿舍狀態</th>
</tr>
<c:forEach items="${homes}" var="home">
<tr>
<td>${home.getId()}</td>
<td>${home.getHomeNum()}</td>
<td>${home.getAddress()}</td>
<td>${home.getDetaileAddress()}</td>
<td>${home.getTel()}</td>
<td>${home.getNum()}</td>
<td>${home.getStatus()}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
這個是Servlet頁面代碼
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
homeDao dao=new homeDao();
ArrayList<Home> homes=dao.getAll();
request.setAttribute("homes", homes);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
這個是資料庫查詢方法
public ArrayList<Home> getAll(){
String sql="select * from tb_home";
try{
conn=DBHelper.getConn();
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
Home home=new Home();
home.setHomeNum(rs.getString("homeNum"));
home.setAddress(rs.getString("address"));
home.setDetaileAddress(rs.getString("detaileAddress"));
home.setTel(rs.getString("tel"));
home.setNum(rs.getInt("num"));
home.setStatus(rs.getInt("status"));
homes.add(home);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBHelper.closeAll(rs, pst, conn);
}
return homes;
}
這個是網頁上輸出結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/241162.html
標籤:Apache
下一篇:web前端
