第一次使用servlet。其中也用了Filter,但是感觉这里主要是servlet路径的问题。所以没有把Filter相关代码贴出来。
web.xml代码如下,其中设置的servlet名为“ValidateServlet”,映射为“/servlet/ValidateServlet”:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
 <filter>
  <filter-name>LocalFilter</filter-name>
  <filter-class>com.study.filter.LocalFilter</filter-class>
  <init-param>
   <param-name>unwelcome</param-name>
   <param-value>worm</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>LocalFilter</filter-name>
  <servlet-name>ValidateServlet</servlet-name>
 </filter-mapping>
 <servlet>
  <description>This is the discription of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>ValidateServlet</servlet-name>
  <servlet-class>com.study.servlet.ValidateServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>ValidateServlet</servlet-name>
  <url-pattern>/servlet/ValidateServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>/index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
在message.jsp里引用的servlet:com.study.servlet.ValidateServlet
关键代码如下:
<form name="form1" method="post" action="/servlet/ValidateServlet">
    <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#0099ff">
    
    <tr bgcolor="#FFFFFF">
    <td colspan="2" align="center"><h2>信息发布</h2></td>
    </tr>
    
    <tr bgcolor="#FFFFFF">
    <td>用户名:</td>
    <td><input type="text" name="username" id="username"/></td>
    </tr>
    
    <tr bgcolor="#FFFFFF">
    <td>标题:</td>
    <td><input type="text" name="username" id="username"/></td>
    </tr>
    
    <tr bgcolor="#FFFFFF">
    <td>内容:</td>
    <td><input type="text" name="username" id="username"/></td>
    </tr>
    
    <tr bgcolor="#FFFFFF">
    <td><input type="submit" name="Submit" value="提交"/>
    <input type="reset" name="Submit2" value="重置"/></td>
    </tr>
    </table>
    </form>
服务器用的Tomcat6.0,从"http://localhost:8080/Test/message.jsp"跳转到"http://localhost:8080/servlet/ValidateServlet"就会报错:
HTTP Status 404 - /servlet/ValidateServlet--------------------------------------------------------------------------------type Status reportmessage /servlet/ValidateServletdescription The requested resource (/servlet/ValidateServlet) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.1
HTTP Status 404是没有文件,所以我觉得是路径错了。不知道是不是这样?

解决方案 »

  1.   

    1:<filter-mapping> 
      <filter-name>LocalFilter </filter-name> 
      <servlet-name>ValidateServlet </servlet-name> 
    </filter-mapping> 
    改成
    <filter-mapping> 
      <filter-name>LocalFilter </filter-name> 
    </filter-mapping> 
    2:
    <form name="form1" method="post" action="/servlet/ValidateServlet"> 
    改成
    <form name="form1" method="post" action="servlet/ValidateServlet"> 
      

  2.   

    1:<filter-mapping> 
      <filter-name>LocalFilter </filter-name> 
      <servlet-name>ValidateServlet </servlet-name> 
    </filter-mapping> 
    改成
    <filter-mapping> 
      <filter-name>LocalFilter </filter-name> 
    </filter-mapping> 
    2:
    <form name="form1" method="post" action="/servlet/ValidateServlet"> 
    改成
    <form name="form1" method="post" action="servlet/ValidateServlet"> 
      

  3.   

    2:
    <form name="form1" method="post" action="/servlet/ValidateServlet">
    改成
    <form name="form1" method="post" action="servlet/ValidateServlet"> 这个,我想也应该是这样。至于
    1: <filter-mapping>
      <filter-name>LocalFilter </filter-name>
      <servlet-name>ValidateServlet </servlet-name>
    </filter-mapping>
    改成
    <filter-mapping>
      <filter-name>LocalFilter </filter-name>
    </filter-mapping> 我认为,应该写<url-pattern></url-pattern>这个标签是必须要写的。
    你可去查查filter-mapping的写法。与<servlet-mapping>中的一样,它应该也要匹配一个url的。
      

  4.   

    应该就是路径的问题,将<form name="form1" method="post" action="/servlet/ValidateServlet"> 
    中的action="/servlet/ValidateServlet"改为action="servlet/ValidateServlet",过滤器的配置应该没影响,
    先改改试试吧!
      

  5.   

    改了。单还是没解决。先拜谢洋葱头~这里我说下,路径我还没弄清楚。关键是写相对路径时class,jsp,xml的“参照物”是哪个啊?
    比如写servlet在myeclipse里的根目录应该是src吧(因为src下面就是com.study.bean,com.study.*,...)?
    但是在硬盘里路径:项目名\WEB-INF\classes\下面的.也就是说..\WEB-INF\classes应该是写的所有类的根目录(我在myeclipse里通过包建立的类都会放到里面)?
    然后写的.jsp文件,是统一放在:项目名  下的,也就是跟..\WEB-INF是一级的,通过IE访问时,如果访问.jsp文件,输入的是
    http://localhost:8080/Test/文件名.jsp
    访问的servlet,路径是:
    http://localhost:8080/名字(形如a/b/c...)那jsp文件跟servlet的根目录就不是一个了吧?
    是不是说对servlet的根目录..\WEB-INF\classes与对jsp的根目录 "..\"要想成同一个目录呢?
    不知道说清楚没~
      

  6.   

    晕了。自己读了遍都感觉头晕~
    访问JSP页面跟servlet的路径,一个多了个项目名,一个没有。
    并且在硬盘目录里一个在
    "D:\Tomcat 6.0\webapps\Test"下
    一个在
    D:\Tomcat 6.0\webapps\Test\WEB-INF\classes\com\study\servlet下
    他们之间是怎样“相对”的?
    这样问好了~
    汗水一地了`
      

  7.   

    还不行的话你这样试试:
    <form name="form1" method="post" action="/项目名/servlet/ValidateServlet"> 
    应该可以!
      

  8.   

    改成 
    <form name="form1" method="post" action="servlet/ValidateServlet"> 
    的情况下,
    http://localhost:8080/Test/message.jsp
    也出现
    HTTP Status 404 - /Test/--------------------------------------------------------------------------------type Status reportmessage /Test/description The requested resource (/Test/) is not available.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.14
      

  9.   

    http://localhost:8080/Test/message.jsp 你这个页面能打开吗?如果打不开的话,看看你的路径对不对。项目名是Test吗,message.jsp是不是在webroot根目录下
      

  10.   

    ZZYYXX1234的回答大家忽略,刚重新测试了下,
    改成<form name="form1" method="post" action="servlet/ValidateServlet"> 
    后编译第一次访问“http://localhost:8080/Test/message.jsp”显示的是HTTP Status 500
    ,所以我觉得路径是没问题了。应该是servlet写的有问题。回头再研究。
    但是为什么第一次访问“http://localhost:8080/Test/message.jsp”显示的是HTTP Status 500
    而第二次访问又会变成HTTP Status 404呢?
    是tomcat内部的问题吗?
    第一次访问“http://localhost:8080/Test/message.jsp”显示如下:
    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Class com.study.servlet.ValidateServlet is not a Servlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    java.lang.Thread.run(Unknown Source)
    root cause java.lang.ClassCastException: com.study.servlet.ValidateServlet cannot be cast to javax.servlet.Servlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    java.lang.Thread.run(Unknown Source)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.14
      

  11.   

    我都干了什么啊~
    纠正下我上面最近那个回复,
    是在“http://localhost:8080/Test/message.jsp”跳转到“http://localhost:8080/servlet/ValidateServlet"时第一次显示HTTP Status 500
    但是第二次访问“http://localhost:8080/Test/message.jsp”时就显示HTTP Status 404,这是为什么?
      

  12.   

    T T又错了~
    是第二次从“http://localhost:8080/Test/message.jsp”跳转到“http://localhost:8080/servlet/ValidateServlet"
    时又显示为HTTP Status 404
    我对不起大家。我去shi了~