这可能是一个愚蠢的问题,但我真的被困住了,需要你的帮助。我制作了一个 Java Servlet,它正在处理对 CRUD 演示应用程序的请求。这是我的小服务程序:
public class AuthorController extends HttpServlet {
private static final long serialVersionUID = 1L;
private AuthorDAO authorDAO;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
authorDAO = new AuthorDAO(jdbcURL, jdbcUsername, jdbcPassword);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
System.out.println(action);
try {
switch (action) {
case "/newAuthor":
showNewForm(request, response);
break;
case "/insertAuthor":
insertAuthor(request, response);
break;
case "/deleteAuthor":
deleteAuthor(request, response);
break;
case "/editAuthor":
showEditForm(request, response);
break;
case "/updateAuthor":
updateAuthor(request, response);
break;
default:
listAuthor(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
/* List all authors */
private void listAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Author> listAuthor = authorDAO.listAllAuthors();
request.setAttribute("listAuthors", listAuthor);
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorList.jsp");
dispatcher.forward(request, response);
}
/* Show authors form */
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
dispatcher.forward(request, response);
}
/* Show authors edit form */
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Author existingAuthor = authorDAO.getAuthor(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
request.setAttribute("author", existingAuthor);
dispatcher.forward(request, response);
}
/* Inserting a new author */
private void insertAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String author_name = request.getParameter("author_name");
int author_age = Integer.parseInt(request.getParameter("author_age"));
String author_country = request.getParameter("author_country");
Author newAuthor = new Author(author_name, author_age, author_country);
authorDAO.insertAuthor(newAuthor);
response.sendRedirect("listAuthor");
}
/* Updating an existing author */
private void updateAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String author_name = request.getParameter("author_name");
int author_age = Integer.parseInt(request.getParameter("author_age"));
String author_country = request.getParameter("author_country");
Author author = new Author(id, author_name, author_age, author_country);
authorDAO.updateAuthor(author);
response.sendRedirect("listAuthor");
}
/* Deleting an existing author */
private void deleteAuthor(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Author author = new Author(id);
authorDAO.deleteAuthor(author);
response.sendRedirect("listAuthor");
}
}
这是我的 web.xml 文件:
<servlet>
<servlet-name>AuthorController</servlet-name>
<servlet-class>org.demo.AuthorController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AuthorController</servlet-name>
<url-pattern>/listAuthor</url-pattern>
</servlet-mapping>
但是,当我尝试访问http://localhost:8080/listAuthor/newAuthor 时, 邮递员给了我一个 404 错误。我究竟做错了什么?我完全迷失了这个映射,无法让它工作。我可以访问:http://localhost:8080/listAuthor并列出我的表和作者,但不能添加新表。