我的Servlet是 DisplayBook.java
package com.book.display;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
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 DisplayBook
*/
@WebServlet("/DisplayBook")
public class DisplayBook extends HttpServlet {
int i;
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DisplayBook() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
i=1;
BufferedReader data=new BufferedReader(new FileReader("C:\\NNK\\Number2\\AddBooks.txt"));
String val;
PrintWriter out=response.getWriter();
out.println("<table border=\"1\" cellspacing=0 cellpadding=5px font:align-center><center><tr><th>Book</th><th>Author</th><th>ISBN</th><th>Genre</th><th>Price</th></tr>");
while((val=data.readLine())!=null)
{
if(i==1)
{
out.println("<tr><td>"+val+"</td>");
}
else if(i==0)
{
out.println("<td>"+val+"</td></tr>");
}
else
{
out.println("<td>"+val+"</td>");
}
i++;
i%=5;
}
out.println("</center></table>");
data.close();
}
}
我的 web.xml 文件是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Library</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DisplayBook</servlet-name>
<servlet-class>DisplayBook</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayBook</servlet-name>
<url-pattern>/DisplayBook/*</url-pattern>
</servlet-mapping>
</web-app>
这是文件的链接
<a href="/DisplayBook/">See the Books</a>
当我尝试点击链接时404 File not found
。我究竟做错了什么?另外,每个元素的正确格式是什么(<servlet-name>
,<servlet-class>
,<url-pattern>
)。我可以找到pattern
,但我找不到name
和class
。这个问题主要针对那些以j2ee
开头的人(像我一样)。另外,请解释这两个元素的一般格式。