【发布时间】:2019-12-05 13:10:28
【问题描述】:
我在 youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) 上关注 derek banas 的教程,当我尝试运行代码时,浏览器显示状态 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。
【问题讨论】:
标签: java xml servlets http-status-code-404 illegalargumentexception