有時(shí)候我們jsp需要展示的頁(yè)面是從數(shù)據(jù)庫(kù)里動(dòng)態(tài)引入的
所以需要先鏈接到一個(gè)servlet,再跳轉(zhuǎn)到j(luò)sp頁(yè)面
那么要怎么做呢
首先使用jsp的動(dòng)態(tài)引入
<jsp:include page="/bookshow"></jsp:include>
url的鏈接就是servlet的鏈接
然后在servlet中獲取數(shù)據(jù)中使用req的跳轉(zhuǎn)
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
BookService bs = new BookServiceImpl();
List<Book> lb = bs.ShowAllBooks();
req.setAttribute("books",lb);
req.getRequestDispatcher("FontMain/bookright.jsp").include(req,resp);
}
注意是include不是reforword
最后顯示的bookright頁(yè)面就是我們所要的頁(yè)面了
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: sz101
Date: 2019/11/10
Time: 9:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link href="../bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="../css/mycss.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<c:forEach items="${requestScope.books}" var = "book">
<div>
<div class = "col-sm-6">
<img class = "img-responsive" src = "${book.image_id}">
<p>${book.image_id}</p>
</div>
<div class = "col-sm-6">
<ul>
<li>${book.name}</li>
<li>${book.author}</li>
<li>${book.description}</li>
<li>${book.price}</li>
</ul>
</div>
</div>
</c:forEach>
</body>
</html>