我目前正在使用 JSP、JSTL、Servlet 等。我有一个问题。我不明白这种情况。
案例1(这个案例非常棒,在JSP文件中显示数据库记录):
小服务程序
@WebServlet("/books")
public class IndexController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Book> books = new BookDAO().getAllBooks();
request.setAttribute("books", books);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
JSP
<c:forEach items="${books}" var="book">
${book}
</c:forEach>
案例2(这个案例不行,JSP文件中不显示数据库记录):
小服务程序
@WebServlet("/")
public class IndexController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Book> books = new BookDAO().getAllBooks();
request.setAttribute("books", books);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
JSP 文件同上。
案例 1:我尝试 localhost/AppName/books => 它有效。
案例 2:我尝试 localhost/AppName/ => 它不起作用
我想在 localhost/AppName 页面显示数据库记录。我该怎么办?