0

ED: this question is not a duplicate. the alleged duplicate makes no mention of the error that was in my console, and the solution to my problem is not found in the linked question.

i'm using derek banas's tutorials to learn servlets right now. or at least, i'm trying to! i keep encountering a 404 error when i submit my form.

here is my code. first, the java class:

package helloservlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Lesson41
 */

@WebServlet
public class Lesson41 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */ 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String usersName = request.getParameter("yourname");

        String theLang = request.getParameter("Language");

        int firstNum = Integer.parseInt(request.getParameter("firstnum"));
        int secondNum = Integer.parseInt(request.getParameter("secondnum"));
        int sumONum = firstNum + secondNum;

        response.setContentType("text/html");

        PrintWriter output = response.getWriter();

        output.println("<html><body><h3>Hello " + usersName);

        output.println("</h3><br />" + firstNum + " + " + secondNum);
        output.println(" = " + sumONum + "<br />Speaks " + theLang + "</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */

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

}

my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>Lesson41</servlet-name>
        <servlet-class>helloservlets.Lesson41</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Lesson41</servlet-name>
        <url-pattern>/Lesson41</url-pattern>
    </servlet-mapping>
</web-app>

and my html:

<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Insert title here</title>
</head>
<body>

    <form method="post" action="http://localhost:8080/Lesson41/">

        What's your name?<br /> <input name="yourname" /><br /> First
        Number <input name="firstnum" /><br /> Second Number <input
            name="secondnum" /><br /> <input type="hidden" name="Language"
            value="English" /><br /> <input type="submit" />

    </form>

</body>
</html>

in my console, i am getting this warning:

WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Lesson41' did not find a matching property.

and that's the only warning i'm getting. i googled it and followed the advice i found here, but it hasn't fixed my problem.

thank you in advance for any help!

Community
  • 1
  • 1

1 Answers1

1

Are you sure the url in the form is correct? http://localhost:8080/Lesson41/ If you are using Eclipse, by default it deploys your application as http://localhost:8080/[Your-Project-Name-Here]/Lesson41/

GiDar
  • 143
  • 2
  • 5