1

I don't know why this is coming up as 404 but my other servlet that's in the same package works just fine. I must be missing something simple here. Please let me know if you see anything wrong. I'm going through the Murach java servlets book and started having this issue and I have no idea what I did wrong

When I try to access AddEmailListServlet via the mapping /AE I get the 404 error. If I try to access /Emailsss that servlet loads up just fine. No idea why??

<?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>

And the AE mapping doesn't want to work to AddEmailListServlet. Here's the servlet code:

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>
}
JKK
  • 436
  • 2
  • 11
  • 22
  • Starting to narrow it down, my mapping is fine, something else is wrong in the class file. Will keep digging – JKK Apr 22 '13 at 22:39
  • As noted below, was simply missing: @WebServlet(name = "AES", urlPatterns = {"/AES"}) – JKK Apr 24 '13 at 21:44

1 Answers1

1

Your servlet is not a servlet. It doesn't extend HttpServlet. I'm even surprised that the webapp deploys without error given this mistake. Check the logs.

Always annotate your methods with @Override when your intention is that they override a superclass or interface method. The compiler would have generated an error if you had done that, warning you that the method didn't override anything.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • oh duh, well I knew it was going to be something simple. Thanks! – JKK Apr 22 '13 at 21:58
  • Made changes as noted by JB but still no go, navigating to AE still return 404 but going to the other servlet is just fine – JKK Apr 22 '13 at 22:02
  • Check the logs. Use a debugger to see if your servlet is invoked. Remove the call to doPost() from doGet() to check if the problem is not with the forward to the JSP. – JB Nizet Apr 22 '13 at 22:05
  • logs just show the app started, no errors. Debugger with break points anywhere in the class of course will not work because the file is not found. That's why I'm getting 404. Not sure how modifying doGet and doPost would do anything to a file that can't be found? I'll show this to some guys at work, maybe they can help find the issue. Any other ideas please post away – JKK Apr 22 '13 at 22:15
  • My idea was that maybe you got a 404 because the JSP you're forwarding to wasn't found. Which URLs are you using to execute your 2 servlets? – JB Nizet Apr 23 '13 at 06:48
  • Sorry slow response, had some other things I had to do. Here's first URL that works fine: http://localhost:8080/testApp/Emailsss and the second is: http://localhost:8080/testApp/AE which doesn't work. Looking at it some more now, see what I can find – JKK Apr 24 '13 at 21:35
  • Found it, was also missing @WebServlet(name = "AES", urlPatterns = {"/AES"}) prior to the class declaration in the AES.java file – JKK Apr 24 '13 at 21:43