0

I am taking a commercial Java programming course at a bootcamp.

I am suppose to construct a single servlet base webpage to verify SSN.

Once the SSN is verified, I then display information about the SSN to the user.

However, I am receiving a 404 page when I run my servlet on Eclipse.

I am trying to debug, but I can't fix the issue.

My confusion lies in passing the parameter from the doGet to the doPost method, to then display the relevant information about the SSN via the doPost.

I am not using an HTML redirection, nor am I redirecting to another servlet.

This is a self-contained servlet.

Code below:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/AboutMe")
public class AboutMe extends HttpServlet {
private static final long serialVersionUID = 1L;


public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");

    out.println("   <meta charset=\"UTF-8\">");
    out.println("   <title>About Me</title>");
    out.println("</head>");
    out.println("<body>");

    out.println("<form action=\"AddEntry\" method=\"get\">");
    out.println("  SSN: <input type=\"text\" name=\"ssn\" /> <br />");
    out.println("  <input type=\"submit\" />");
    out.println("</form>");

    out.println("</body>");
    out.println("</html>");
}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    String SSN = request.getParameter("ssn");


    //validate string
String message = "";
    if ( SSN == null || SSN.trim().length() == 0 || SSN.trim().length() !=     9 || !SSN.equals("123456789") ) { 
message = "Enter SSN!";
} else {


    response.setContentType( "text/html" );

    PrintWriter out = response.getWriter();
    out.println( "<html>");
    out.println("<head>"); 
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");

    out.println("   <meta charset=\"UTF-8\">");
    out.println("   <title>Insert title here</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<div id = name>");
    out.println("FirstName LastName");
    out.println("</div>");
    out.println("</br>");
    out.println("</br>");
    out.println( "<p>The SSN is " + SSN + "! </p>" );
    out.println("</br>");
    out.println("</br>");
    out.println("<p class = paragraph>");
    out.println(" User information goes here." );

    out.println( "</body></html>" );
    }


    }

}
  • You should probably make that `` – Elliott Frisch Sep 18 '16 at 04:42
  • Thank you. This worked because I now see the entered parameter in the url, but I cannot display the information from the doPost method. Once I enter the SSN, a 404 page is returned, but the url accepts the entered parameter. So, I get a url with the entered SSN, but the URL displays a 404 page. – BIG_MAC_AND_FRIES Sep 18 '16 at 04:46
  • Sounds like you want to remove `request.getRequestDispatcher("AboutME").forward(request, response);` (which is actually a better design pattern, but isn't for mixing with your `out.println` calls). – Elliott Frisch Sep 18 '16 at 04:48
  • @ElliottFrisch, I have removed the request.getRequestDispatcher("AboutMe").forward(request, response);, but I still receive a 404 error. The parameter is being accepted by doGet, but doPost is returning a 404 page. – BIG_MAC_AND_FRIES Sep 18 '16 at 04:52

1 Answers1

0

Your servlet is only handling /AboutMe as per the line @WebServlet("/AboutMe") but your form action points to the address /AddEntry.

Either change the form action also to /AboutMe and method POST or add a handler for /AddEntry (and also change the form method to POST) with

@WebServlet(ulPatterns={"/AboutMe","/AddEntry"})

Mark Olsson
  • 320
  • 2
  • 8
  • thank you so much! That fixed the problem. I wasn't aware that such a minor change in the form submission would have such a great impact on my overall servlet. – BIG_MAC_AND_FRIES Sep 18 '16 at 05:29