-1

My web program will allow a user to enter first name and last name and after the user press the submit button, it will print the first name and last name.

I receive an HTTP error 404 after the user presses the "Submit" button.

enter image description here

Error

enter image description here

This is my File structure

enter image description here

My codes

PrintFormContent.java

package form;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/PrintFormContent")
public class PrintFormContent extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PrintFormContent() {
        super();
    }

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String fname = request.getParameter("name");
        String lname = request.getParameter("lname");

         try{
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.print("<p>First Name</p>" + fname);
                out.print("<p>Last Name</p>" + lname);

            }catch(IOException e){
                e.printStackTrace();
            }   }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
    <form id="PrintFormContent" name="PrintFormContent" role="form"
        action="/testForm/form/PrintFormContent" method="POST">
        <div class="form-group row">
            <label for="fname" class="col-md-2">First Name</label>
            <div class="col-md-4">
                <input type="text" class="form-control" id="fname"
                    placeholder="Enter First Name" name="fname" />
            </div>
        </div>
        <div class="form-group row">
            <label for="lname" class="col-md-2">First Name</label>
            <div class="col-md-4">
                <input type="text" class="form-control" id="lname"
                    placeholder="Enter Last Name" name="lname" />
            </div>
        </div>
        <div style="text-align: center;">
            <button id="btn" type="submit" class="btn btn-default">Submit</button>
        </div>
    </form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>testForm</display-name>
  <servlet>
    <servlet-name>PrintFormContent</servlet-name>
    <servlet-class>PrintFormContent</servlet-class>
  </servlet>

  <welcome-file-list>
    <welcome-file>/testForm/program/form/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Please Help.

T-Heron
  • 5,385
  • 7
  • 26
  • 52
user3820292
  • 310
  • 3
  • 19

1 Answers1

0

you declared your servlet to PrintFormContent but you send your request to /testForm/form/PrintFormContent. You need to change it in the form-declaration:

<form id="PrintFormContent" name="PrintFormContent" role="form"
    action="/PrintFormContent" method="POST">

btw. don't forget to change parameter name in the servlet to fname:

String fname = request.getParameter("fname");

try to add this to web.xml, somehow your anotation seems not to work

<servlet-mapping>
   <servlet-name>PrintFormContent</servlet-name>
   <url-pattern>/PrintFormContent</url-pattern>
</servlet-mapping>

check this, maybe you have the same problem: webservlet-annotation-doesnt-work-with-tomcat-8

Community
  • 1
  • 1
JohnnyAW
  • 2,866
  • 1
  • 16
  • 27