日期:2014-05-18  浏览次数:20715 次

Servlet程序问题
我写了一个Servlet的程序如下
C# code

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CounterServlet extends HttpServlet 
{
    private static final String CONTENT_TYPE = "text/html; charset=GBK";
    private int number;
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
    {
        response.setContentType (CONTENT_TYPE);

        PrintWriter out = response.getWriter();    int n = 1;
        number = Integer.parseInt(request.getParameter("DATA"));
        for (int i = 1; i <= number; i++) 
        { 
            n *= i;    
        }
        out.println("<h2 align='center'>" + "计算结果" + "</h2>");
        out.println("<html>");
        out.println("<head><title>TestServlet</title></head>");
        out.println("<body>");
        out.println("<P>"+number +"! = " + n + "</P>");
        out.println("</body></html>");
        out.close();    
    }
}


然后 javac CounterServlet.java编译后放在D:\tomcat\webapps\ROOT\coun\WEB-INF\CLASSES这个文件夹里
然后在D:\tomcat\webapps\ROOT\coun\WEB-INF这个文件夹里放一个WEB.XML文件,文件内容为
C# code

<servlet>
  <servlet-name>CounterServlet</servlet-name>
  <servlet-class>CounterServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>CounterServlet</servlet-name>
  <url-pattern>/CounterServlet</url-pattern>
</servlet-mapping>


然后再在D:\tomcat\webapps\ROOT\coun中方一个coun.html文件
文件内容为
HTML code

<html>
<head>
<title>网上购物</title>
</head>
<body bgcolor="white">
<table border="10" cellpadding="10" align="CENTER">
<tr><td width="275">
<h2>商品信息查询</h2>
<br>输入你要查询的信息<br>
<form method="post" action="CounterServlet"> 
<input type= "text" name="DATA" size=30> 
<p>
<input type="SUBMIT" value= "查询"> 
<input type="RESET" value= "重新输入"> 
</form> 
</td></tr> 
</table> 
</body> 
</html>


我运行http://localhost:8080/和http://localhost:8080/coun/coun.html都可以显示
但是在coun.html中输入5点查询,不能显示结果,??
显示下面的错误
HTTP Status 404 - /coun/CounterServlet

--------------------------------------------

type Status report

message /coun/CounterServlet

description The requested resource (/coun/CounterServlet) is not available.


--------------------------------------------

Apache Tomcat/5.5.26

问了好多人都不知道原因??
帮帮我啊,我弄了4天了~~

------解决方案--------------------
CounterServlet 这个类加上包结构

package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CounterServlet extends HttpServlet 
。。。。。。。。。。

编译后 吧 这个CounterServlet.class 

放到 D:\tomcat\webapps\ROOT\coun\WEB-INF\CLASSES\test 下面

<servlet-class>CounterServlet</servlet-class>


改成<servlet-c