我有一个Java servlet作为
package com.nh.bookapp;
import com.books.Book;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BookApp
*/
@WebServlet("/BookApp")
public class BookApp extends HttpServlet {
private static final long serialVersionUID = 1L;
List <Book> Books = new ArrayList<Book>();
/*List<String> BookNames = new ArrayList<String>();
List<String> Authors = new ArrayList<String>();
List<String> Costs = new ArrayList<String>();*/
/**
* @see HttpServlet#HttpServlet()
*/
public BookApp() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String bookName = request.getParameter("bookname");
String author = request.getParameter("authorname");
String bookCost = request.getParameter("cost");
String url = ("");
/*BookNames.add(bookName);
Authors.add(author);
Costs.add(cost);*/
Book newBook = new Book();
if(bookName.length()!=0&&author.length()!=0&&bookCost.length()!=0)
{
newBook.authorName=author;
newBook.name=bookName;
newBook.cost = Float.parseFloat(bookCost);
Books.add(newBook);
request.setAttribute("Book", newBook);
url =("/displayBook.jsp");
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Book类定义为: 打包com.books;
public class Book {
public String name;
public String authorName;
public Float cost;
}
index.html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
</head>
<body>
<form name="BookForm" id="fBook" action="BookApp" method="post">
Name:
<input type="text" name="bookname" id="tbBook">
<br>
Author:
<input type="text" name="authorname" id="tbAuthor">
<br>
Cost:
<input type="text" name="cost" id="tbCost">
<br>
<input type ="submit" value="Create">
</form>
</body>
</html>
displayBook.jsp是:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
</head>
<body>
<form name="BookForm" id="fBook" action="BookApp" method="post">
Name:
<input type="text" name="bookname" id="tbBook">
<br>
Author:
<input type="text" name="authorname" id="tbAuthor">
<br>
Cost:
<input type="text" name="cost" id="tbCost">
<br>
<input type ="submit" value="Create">
</form>
</body>
</html>
运行服务器时,我在根本原因下遇到错误
javax.el.PropertyNotFoundException: Property [name] not found on type [com.books.Book]
我在JSP文件中具有必要的导入,并且变量在类中声明,但是尽管它们是公共的,但我仍然无法访问它们。
我无法弄清楚哪里出了问题。我尝试使用
<c:out value="${Book.name}" />
就像在另一个答案中建议的那样,但是没有用。