なぜこれが 404 になるのかはわかりませんが、同じパッケージにある他のサーブレットは問題なく動作します。ここで単純なものが欠けているに違いありません。何か問題がありましたらお知らせください。Murach Java サーブレットの本を読んでいて、この問題が発生し始めました。何が間違っていたのかわかりません。
マッピング /AE を介して AddEmailListServlet にアクセスしようとすると、404 エラーが発生します。/Emailsss にアクセスしようとすると、サーブレットは正常にロードされます。理由がわからない??
<?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 に対して機能したくありません。サーブレットのコードは次のとおりです。
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>
}