我不知道为什么会出现 404 但我在同一个包中的另一个 servlet 工作得很好。我必须在这里遗漏一些简单的东西。如果您发现任何问题,请告诉我。我正在阅读 Murach java servlets book 并开始遇到这个问题,但我不知道我做错了什么
当我尝试通过映射 /AE 访问 AddEmailListServlet 时,我收到 404 错误。如果我尝试访问 /Emailsss 那个 servlet 加载就好了。不知道为什么??
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>AddEmailListServlet</servlet-name>
<servlet-class>email.AddEmailListServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>EmailServlet</servlet-name>
<servlet-class>email.EmailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEmailListServlet</servlet-name>
<url-pattern>/AE</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EmailServlet</servlet-name>
<url-pattern>/Emailsss</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/error_404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error_java.jsp</location>
</error-page>
</web-app>
并且 AE 映射不想对 AddEmailListServlet 起作用。这是servlet代码:
package email;
import business.User;
import data.UserIO;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
public class AddEmailListServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AddToEmailListServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AddToEmailListServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
PrintWriter out = response.getWriter();
out.println("Hello from AddEmailListServlet.java");
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
ServletContext sc = request.getServletContext();
String path = sc.getRealPath("/WEB-INF/EmailList.txt");
User user = new User(firstName, lastName, emailAddress);
UserIO.add(user, path);
HttpSession session = request.getSession();
session.setAttribute("user", user);
String url = "/ch09/dispay_email_entry.jsp";
RequestDispatcher dispatcher =
request.getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
//processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}