我有一个问题,我试图运行我的简单servlet,名为Hello.java。我在eclipse中创建了它,然后将文件放置在webapps/ServletTest/WEB-INF/classes
文件夹中并编译它,在同一文件夹中创建了Hello.class
文件。然后我修改了我的web.xml文件以映射servlet,并尝试通过以下地址运行它
http://localhost:8080/ServletTest/Hello
然而,这不起作用,会出现以下错误
HTTP状态404 -
类型状态报告
消息
说明 请求的资源不可用。Apache Tomcat / 7.0.42
web.xml文件中的映射如下:
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Main.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
Servlet的代码:
package Main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String path = request.getContextPath();
String ip = request.getRemoteAddr();
out.print("<html>" +
"<title>Hello</title>" +
"Hello World"+ "<br>" +
"Your ip is: " + ip + "<br>" +
"Your path is: " + path
+ "</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
web.xml
中声明你的应用程序正在使用Servlet 3.0。 - Sotirios Delimanolis