我不知道为什么这会出现在404,但我在同一个包中的其他servlet工作得很好。我必须在这里遗漏一些简单的东西。如果您发现任何错误,请告诉我。我正在阅读Murach java servlets一书并开始讨论这个问题,我不知道我做错了什么
当我尝试通过映射/ 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>
}
答案 0 :(得分:1)
您的servlet不是servlet。它没有扩展HttpServlet。我甚至感到惊讶的是,由于这个错误,webapp部署没有错误。检查日志。
当您的意图是覆盖超类或接口方法时,始终使用@Override注释您的方法。如果您已经这样做,编译器会生成错误,警告您该方法没有覆盖任何内容。