我的Servlet执行不了啊,求大家看看
我是看尚学堂科技_张志宇学Servlet的,可是我的Servlet死活运行不了,下面说一下我的配置,大家帮我看看。
首先我是在tomcat的webapp下创建了一个目录myapp,然后在myapp下又写了一个index.html。并且建了一个WEB-INF里面有一个web.xml,文件内容如下。
<?xml version="1.0" encoding="utf8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldServlet是我写的Servlet,在WEB-INF的classes下,文件内容如下
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = -6917571878047982497L;
/**
* @param args
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
PrintWriter out=resp.getWriter();
out.println("<html><head><title>enter my web</title></head><body>Hello World!!!</body></html>");
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
可是问题就来了,每次我输入http://localhost:8080/myapp/就可以看到我写的index.html的内容,但是我输入http://localhost:8080/myapp/MyServlet,就会出现这样的错误
HTTP Status 404 - /myapp/MyServlet
type Status report
message /myapp/MyServlet
description The requested resource is not available.
那位好心人帮我看看我的这个配置有什么问题。
HelloWorldServlet.class 在WEB-INF/classes目录下,这个应该没问题。
servlet webapp
[解决办法]
把 super.doGet(req, resp); 去掉试一试