0

I'm learning Servlet but stuck on this 404 a whole day after many times check. I'm using myelcipse to edit and deploy automatically. Here is my three files code, including:

1. An "addEmpPage" html for data entrance and form submit:

<!DOCTYPE html>
<html>
  <head>
    <title>addEmpPage</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head>
  <body style="font-size:24px;">
    <div>
        <h1>Add Employer Page:</h1>
    </div>
    <div>
        <form action="http://localhost:8080/AddEmpClass/addEmpUrl" method="post">
            <fieldset>
                <legend>Add New Employee</legend>
                name:&nbsp;<input name="name"/><br>
                salary:&nbsp;<input name="salary"/><br>
                age:&nbsp;<input name="age"/><br>
                <input type="submit" value="add"/>              
            </fieldset>
        </form>
    </div>
  </body>
</html>

2. A web.xml for servlet mapping:

<?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">
    <servlet>
        <servlet-name>add</servlet-name>
        <servlet-class>addemppackage.AddEmpClass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>add</servlet-name>
        <url-pattern>/addEmpUrl</url-pattern>
    </servlet-mapping>
</web-app>

3. A java file for logic:

package addemppackage;

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

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.http.HTTPException;

public class AddEmpClass extends HttpServlet {
    public void service(HttpServletRequest req, HttpServletResponse res)
            throws HTTPException, IOException{
        req.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
        PrintWriter out=res.getWriter();
        String name=req.getParameter("name");
        double sal=Double.valueOf(req.getParameter("salary"));
        int age=Integer.valueOf(req.getParameter("age"));

        out.println("<h1>Employee Information</h1>");
        out.println("<h2>name:"+name+"</h2>");
        out.println("<h2>salary:"+sal+"</h2>");
        out.println("<h2>age:"+age+"</h2>");
        out.close();

    }
}

Here is the file structure:

[file structure]1

I deploy and run the server, and after I fill the blanks and submit the data, the page jumps to the 404 error page: The requested resource is not available.

u32i64
  • 2,384
  • 3
  • 22
  • 36
Kes
  • 1
  • 2
  • And, this also happened when I change the action path of form to the relative path. – Kes Mar 05 '17 at 07:14
  • 1
    Sure 'AddEmpClass' is the correct context path (the portion after the port number)? After looking at your screenshot I bet it is 'addemp' – home Mar 05 '17 at 07:15
  • Thx~a stupid mistake@home – Kes Mar 05 '17 at 07:32

0 Answers0