我是servlet编程的新手。我的tomcat在eclipse和main系统中运行良好。当我尝试运行servlet时,它显示404错误。我使用动态Web项目制作了我的servlet。我的代码是:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class amar extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello friend!");
response.getStatus();
}
}
请帮我解决这个问题。我的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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>first servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:1)
在web.xml中添加servlet的映射。
<servlet>
<servlet-name>amar </servlet-name> // name of servlet
<servlet-class>packagename.amar </servlet-class> //packagename.class name
</servlet>
<servlet-mapping>
<servlet-name>amar </servlet-name>
<url-pattern>/amar </url-pattern>
</servlet-mapping>
从浏览器运行它 本地主机:8080 /项目名称/阿玛尔