菜鸟发问:我第一次创建servlet,运行不了,问题在哪里?
以前只会jsp+bean,今天,按书上写的,在tomcat/webapp/下创建了servletdemo文件夹,其下创建WEB-INF,其下建classes.
然后在classes中建立了文件servletdemo.java,并用javac -classpath d:\tomcat\common\lib\servlet.api.jar servletdemo.java命令成功编译.
接下来在WEB-INF下建立了web.xml文件
最后在ie地址栏输入http://127.0.0.1:8081/servletdemo/servletdemo,却发现不能成功,报错:
HTTP Status 404 - /servletdemo/servletdemo
type Status report
message /servletdemo/servletdemo
description The requested resource (/servletdemo/servletdemo) is not available.
Apache Tomcat/5.0.28
请求大家帮助我,我可是完全按照书上来的哦,怎么不行呢?
附文件
servletdemo.java内容如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class servletdemo extends HttpServlet{
private static final String content_type="text/html;charset=gbk";
public void init() throws
ServletException{
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(content_type);
PrintWriter out =response.getWriter();
out.println("<html>");
out.println("<head><title>servlet示例</title></head>");
out.println("<body>");
out.println("<h3>servlet示例.</h3>");
out.println("</body></html>");
}
public void destroy(){}
}
web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE webapp PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application
2.3//EN"
"http://java.sun.com/dtd/web-app-23.dtd">
<web-app>
<displayname>servletdemo</display-name>
<servlet>
<servlet-name>servletdemo</servlet-name>
<servlet-class>ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletdemo</servlet-name>
<url-pattern>/servletdemo</url-pattern>
</servlet-mapping>
</web-app>
------解决方案--------------------
web.xml中配置有误,你的类名是:servletdemo,全是小写字符
而<servlet-class> ServletDemo </servlet-class>
指定的类与定义的类不一致。
改成:
<servlet-class> servletdemo </servlet-class>