我正在关注youtube(https://www.youtube.com/watch?v=_HnJ501VK3M)上的derek banas的教程,当我尝试运行代码时,浏览器将显示status-404和

讯息:/ Lesson41 /

描述:原始服务器找不到目标资源的当前表示,或不愿意透露其存在。

我得到了IllegalArguementsException,它说了一些关于2个不同的servlet映射到相同url模式的问题。所以我删除了@WebServlet注释。
现在我正在获取404,但我不知道是什么原因。我认为eclipse看不到sayhello.html

这是代码:
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);
    }
}


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>


目录结构:
directory Structure

预期产量:
它应该显示一个表单,其中包含名称,数字1,数字2和提交按钮的字段。它正确地转到localhost:8080 / Lesson41 /,但是看不到html。

最佳答案

将url模式更新为/ Lesson41 /

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


将动作更改为/ Lesson41 /并尝试。可能会帮助您。

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

</form>


另外,在映射时,您将servlet类用作“ helloservlets.Lesson41”,则需要添加该包。 servlet'Lesson41'中的'helloservlets'。

09-10 16:21