想用java做电影推荐网站,网站首页是jsp,想在首页显示数据库里的电影信息,应该怎么操作?
是用servlet吗,我在servlet写了个读取数据库电影信息并输出,存到了list,然后jsp里怎么转到servlet并显示信息呀
想要实现成主页大概是这个样子
这有个案例,流程就是读取数据库中的数据显示到jsp页面,很简单
将查询的数据库所有用户信息封装到user中,存储到request域中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>用户信息管理系统</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
td, th {
text-align: center;
}
</style>
<script type="text/javascript">
function deluser(i) {
//用户安全提示
if(confirm("您确定需要删除吗?")){
location.href="${pageContext.request.contextPath }/delUserServlet?id="+i;
}
}
</script>
</head>
<body>
<div class="container">
<h3 style="text-align: center">用户信息列表</h3>
<div style="float: left;">
<form class="form-inline">
<div class="form-group">
<label for="exampleInputName2">姓名</label>
<input type="text" class="form-control" id="exampleInputName2" >
</div>
<div class="form-group">
<label for="exampleInputName3">籍贯</label>
<input type="text" class="form-control" id="exampleInputName3" >
</div>
<div class="form-group">
<label for="exampleInputEmail2">邮箱</label>
<input type="email" class="form-control" id="exampleInputEmail2" >
</div>
<button type="submit" class="btn btn-default">查询</button>
</form>
</div>
<div style="float: right; margin: 5px;">
<a class="btn btn-primary" href="${pageContext.request.contextPath }/add.jsp">添加联系人</a>
<a class="btn btn-primary" href="delete.jsp">删除选中</a>
</div>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th><input type="checkbox"></th>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>QQ</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="user" varStatus="s">
<tr>
<td><input type="checkbox"></td>
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.gender }</td>
<td>${user.age }</td>
<td>${user.address }</td>
<td>${user.qq }</td>
<td>${user.email }</td>
<td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath }/queryupdate?id=${user.id}">修改</a> <a class="btn btn-default btn-sm"
href="javascript:deluser(${user.id});" >删除</a></td>
</tr>
</c:forEach>
</table>
<div>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<span style="font-size: 25px;margin-left: 5px;">
共16条记录,共4页
</span>
</ul>
</nav>
</div>
</div>
</body>
</html>
展示效果:
注意:这里修改,删除,添加功能还没实现,只是单纯显示所有用户这个页面
解决方案:
public class MovieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 读取数据库中的电影信息
List<Movie> movies = MovieDao.getMovies();
// 将电影信息存储到request域中
request.setAttribute("movies", movies);
// 转发到jsp页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
<%
// 调用MovieServlet获取电影信息
request.getRequestDispatcher("/MovieServlet").include(request, response);
// 获取电影信息
List<Movie> movies = (List<Movie>)request.getAttribute("movies");
%>
<table>
<tr>
<th>电影名称</th>
<th>类型</th>
<th>演员</th>
<th>评分</th>
</tr>
<% for (Movie movie : movies) { %>
<tr>
<td><%= movie.getName() %></td>
<td><%= movie.getType() %></td>
<td><%= movie.getActor() %></td>
<td><%= movie.getScore() %></td>
</tr>
<% } %>
</table>
<%
// 调用MovieServlet获取电影信息及分页信息
request.getRequestDispatcher("/MovieServlet?page=<%= request.getParameter("page") %>&pageSize=<%= request.getParameter("pageSize") %>").include(request, response);
// 获取电影信息和分页信息
List<Movie> movies = (List<Movie>)request.getAttribute("movies");
PageBean pageBean = (PageBean)request.getAttribute("pageBean");
%>
<table>
<tr>
<th>电影名称</th>
<th>类型</th>
<th>演员</th>
<th>评分</th>
</tr>
<% for (Movie movie : movies) { %>
<tr>
<td><%= movie.getName() %></td>
<td><%= movie.getType() %></td>
<td><%= movie.getActor() %></td>
<td><%= movie.getScore() %></td>
</tr>
<% } %>
</table>
<div>
<% if (pageBean.isHasPreviousPage()) { %>
<a href="?page=<%= pageBean.getPreviousPage() %>&pageSize=<%= pageBean.getPageSize() %>">上一页</a>
<% } %>
<% if (pageBean.isHasNextPage()) { %>
<a href="?page=<%= pageBean.getNextPage() %>&pageSize=<%= pageBean.getPageSize() %>">下一页</a>
<% } %>
<% if (pageBean.isHasLastPage()) { %>
<a href="?page=<%= pageBean.getLastPage() %>&pageSize=<%= pageBean.getPageSize() %>">末页</a>
<% } %>
<% if (pageBean.getTotalPages() > 1) { %>
<select onchange="location.href='?page=<%= pageBean.getCurrentPage() %>&pageSize='+this.options[this.selectedIndex].value">
<option value="5" <%= pageBean.getPageSize() == 5 ? "selected" : "" %>>每页显示5条</option>
<option value="10" <%= pageBean.getPageSize() == 10 ? "selected" : "" %>>每页显示10条</option>
<option value="20" <%= pageBean.getPageSize() == 20 ? "selected" : "" %>>每页显示20条</option>
</select>
<% } %>
</div>
public class MovieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取分页信息
int page = Integer.parseInt(request.getParameter("page"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
// 获取电影信息和分页信息
List<Movie> movies = MovieDao.getMoviesByPage(page, pageSize);
int totalCount = MovieDao.getTotalCount();
PageBean pageBean = new PageBean(page, pageSize, totalCount);
// 将电影信息和分页信息存储到request域中
request.setAttribute("movies", movies);
request.setAttribute("pageBean", pageBean);
// 转发到jsp页面
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
其中分页的处理可以参考以下代码:
public class MovieDao {
public static List<Movie> getMoviesByPage(int page, int pageSize) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Movie> movies = new ArrayList<Movie>();
try {
conn = DBUtil.getConnection();
String sql = "select * from Movie limit ?, ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, (page - 1) * pageSize);
pstmt.setInt(2, pageSize);
rs = pstmt.executeQuery();
while (rs.next()) {
Movie movie = new Movie();
movie.setId(rs.getInt("id"));
movie.setName(rs.getString("name"));
movie.setType(rs.getString("type"));
movie.setActor(rs.getString("actor"));
movie.setScore(rs.getDouble("score"));
movies.add(movie);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, pstmt, conn);
}
return movies;
}
public static int getTotalCount() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int totalCount = 0;
try {
conn = DBUtil.getConnection();
String sql = "select count(*) from Movie";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
totalCount = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, pstmt, conn);
}
return totalCount;
}
}
其中PageBean的实现可以参考以下代码:
public class PageBean implements Serializable {
private int currentPage;
private int pageSize;
private int totalCount;
private int totalPages;
public PageBean(int currentPage, int pageSize, int totalCount) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
if (this.totalCount % this.pageSize == 0) {
this.totalPages = this.totalCount / this.pageSize;
} else {
this.totalPages = this.totalCount / this.pageSize + 1;
}
if (this.currentPage < 1) {
this.currentPage = 1;
} else if (this.currentPage > this.totalPages) {
this.currentPage = this.totalPages;
}
}
public int getPreviousPage() {
return this.currentPage - 1;
}
public int getNextPage() {
return this.currentPage + 1;
}
public int getLastPage() {
return this.totalPages;
}
public int getCurrentPage() {
return this.currentPage;
}
public int getPageSize() {
return this.pageSize;
}
public int getTotalCount() {
return this.totalCount;
}
public int getTotalPages() {
return this.totalPages;
}
public boolean isHasPreviousPage() {
return this.currentPage > 1;
}
public boolean isHasNextPage() {
return this.currentPage < this.totalPages;
}
public boolean isHasFirstPage() {
return this.currentPage != 1;
}
public boolean isHasLastPage() {
return this.currentPage != this.totalPages;
}
}