我正在尝试使用eclipse中的tomcat 8服务器创建Java Web应用程序。 我已经创建了servlet,它将请求转发到jsp页面并使用来自两个表的数据初始化该页面内的表。我一直在互联网上关注如何用MySQL DB中的recoreds填充html表但是失败的几个例子。我正在使用MainServlet的doGet方法将数据从我的本地数据库中获取到列表中,并在请求中将其设置为属性。但是当我启动servlet时,jsp表中没有记录。 Servlet连接到db并从中接收记录(我已经调试过app)。那我的代码有什么问题?我是html和css,servlet和jsp技术的新手。也许问题是我正在使用的css框架(Materialise CSS)。 这是我的MainServlet.java代码:
package redirect;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 java.sql.Connection;
import util.Book;
@WebServlet(name="Libbook",urlPatterns={"/libbook"})
public class MainServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private static String url = "jdbc:mysql://localhost:3306/Test";
private static String user = "user",password = "password";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<Book> books = new ArrayList<>();
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,user,password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT IDBook,Title,AddDate,ISBN,Name,Sirname "
+ "FROM Book INNER JOIN Author "
+ "ON Book.IDAuthor = Author.IDAuthor;");
while (resultSet.next())
{
Book b = new Book();
b.setId(resultSet.getInt(1));
b.setTitle(resultSet.getString(2));
b.setIsbn(resultSet.getLong(4));
b.setDate(resultSet.getDate(3).toString());
b.setName(resultSet.getString(5));
b.setSirname(resultSet.getString(6));
books.add(b);
}
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
finally {
try {if(connection != null)connection.close();}catch (SQLException e) {e.printStackTrace();}
}
request.setAttribute("books", books);
request.getRequestDispatcher("pages/libbook.jsp").forward(request, response);
}
}
libbook.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8"/>
<title>LibBook</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<style type="text/css">
.logo{
color: #444;
text-transform: uppercase;
letter-spacing: 0.3em;
font-size: 2em;
}
.logo img{
width: 5%;
margin: 0.5%;
vertical-align: middle;
margin-right: 0.5em;
}
</style>
</head>
<body>
<nav class="white">
<div class="navbar-wrapper container">
<a id="logo-container" href="#" class="logo">
LIBBOOK
<img src="assets/book.svg" alt="LibBooklogo">
</a>
<ul class="right">
<li><a class="waves-effect waves-light btn" href="pages/new_author.jsp"><i class="material-icons left">perm_identity</i>NEW AUTHOR</a></li>
<li><a class="waves-effect waves-light btn" href="pages/new_book.jsp"><i class="material-icons left">class</i>NEW BOOK</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="card-panel">
<form action="test">
<div class="input-field">
<input placeholder="Type here (title,author) and press enter.." id="search_bar" type="text">
<label for="search_bar">Search</label>
</div>
</form>
</div>
</div>
<div class="row">
<div class="card-panel">
<table>
<thead>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Info/Edit</th>
<th>Remove</th>
</tr>
</thead>
<c:forEach var="book" items="${books}">
<tr>
<td><c:out value="${book.isbn}"/></td>
<td><c:out value="${book.title}"/></td>
<td><c:out value="${book.name}"/></td>
<td>
<a href=""><i class="material-icons">mode_edit</i></a>
</td>
<td>
<a href=""><i class="material-icons">delete</i></a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
</body>
</html>
图书课程:
package util;
import java.io.Serializable;
public class Book implements Serializable
{
private Integer id;
private String title;
private String date;
private Long isbn;
private String name;
private String sirname;
public Book(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIsbn() {
return isbn;
}
public void setIsbn(Long isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSirname() {
return sirname;
}
public void setSirname(String sirname) {
this.sirname = sirname;
}
}
答案 0 :(得分:0)
找到解决方案。问题出在jsp文件中,我没有包含与c标签scriplet相关的导入:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
并没有将jstl-1.2.jar库添加到我的项目中
答案 1 :(得分:-1)
Java在bean类中查找getter方法来读取数据。 正如你的bean类(Book)getter方法名称就像getISBN(),getTitle(),getAName()等。你应该在jsp中使用代码,如
<td><c:out value="${book.iSBN}"/></td>
<td><c:out value="${book.title}"/></td>
<td><c:out value="${book.aName}"/></td>