1

I am following derek banas' tutorial on youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) and when I try to run the code the browser displays a status-404 and
Message: /Lesson41/
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I was getting IllegalArguementsException saying something about 2 different servlets being mapped to the same url-pattern. So I removed the @WebServlet annotation. Now I'm getting the 404 and I don't know what the cause is. I think eclipse doesn't see the sayhello.html

Here is the code: Servlet: Lesson41.java

public class Lesson41 extends HttpServlet {
    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); 
        output.println("</body></html>");
 }

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

The 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_4_0.xsd" id="WebApp_ID" version="4.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>

html: sayhello.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>

<form method="post" action="http://localhost:8080/Lesson41/">
    What's your name?<br />
    <input name="yourname" /><br />

    First Number<br />
    <input name="firstnum" /><br />

    Second Number<br />
    <input name="secondnum" /><br />

    <input type="hidden" name="Language" value="English" /><br />

    <input type="submit" />
</form>
</body>
</html>

The directory structure: directory Structure

Expected Output: It should show a form with fields for name, number1, number2 and a submit button. It correctly goes to localhost:8080/Lesson41/ but can't see the html.

codeMonkey
  • 11
  • 3

1 Answers1

0

Update the url-pattern to /Lesson41/

<url-pattern>/Lesson41/</url-pattern>

form action change to /Lesson41/ and try. might help you.

<form method="post" action="/Lesson41/">

</form>

Also, while mapping you are using servlet-class as 'helloservlets.Lesson41', need to add the package. 'helloservlets' in servlet 'Lesson41'.

Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11
  • I just tried your suggestion and it gave the same 404 message. Also I checked again. The package is already in the servlet. – codeMonkey Jul 27 '19 at 16:38
  • you can check - https://stackoverflow.com/questions/11731377/servlet-returns-http-status-404-the-requested-resource-servlet-is-not-availa. might be it will help you. – Shivendra Singh Jul 27 '19 at 16:56