我是Java的新手。这是我用jsp和servlet开发的第一个应用程序。
当我单击提交按钮时,结果显示为http status 404
。
错误是
HTTP状态404 –找不到
[Type状态报告
消息 / DemoApp /添加
Description原始服务器未找到目标资源的当前表示,或不愿意透露该资源的存在。
在web.xml中,
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.saunghninoo.AddServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet>
Index.html
<form action="add" method="post">
Enter 1st no: <input type="text" name="num1" class="form-control"><br>
Enter 2nd no: <input type="text" name="num2" class="form-control"><br>
<input type="submit" class="btn btn-primary">
</form>
IN AddServlet.java,
package com.saunghninoo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k= i+j;
PrintWriter out = res.getWriter();
out.println("result is "+ k);
}
}
请帮帮我!
在web.xml中,像这样更改
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.saunghninoo.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>