Search code examples
javaservletsbootcamp

How to pass a form submission using parameters in doGet to doPost on a Java servlet?


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>" );
    }


    }

}

Solution

  • 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"})