在自学servlet的菜鸟
跟书上一步一步做然后tomcat就死活访问不了了。
下面是project的结构下面是web.xml...
<?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">
  
  <servlet>
    <servlet-name>FirstServlet</servlet-name>
    <servlet-class>FirstServlet</servlet-class>
  </servlet>  <servlet-mapping>
    <servlet-name>FirstServlet</servlet-name>
    <url-pattern>/servlet/FirstServlet</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
下面是FirstServlet.javaimport 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 FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is a servlet ");
out.println("  </BODY>");
out.println("</HTML>");
}}加了servlet后,连起初可以访问的index都访问不了了。。
问题很简单求大虾指点迷津

解决方案 »

  1.   

    你看下是不是没有部署上去,或者是tomcat没有起来。
      

  2.   

    servlet没有package的吧,如果有的话得加上你的tomcat没启动成功吧?
      

  3.   

    tomcat启动了~~http://localhost:8080/没问题的~~
    myeclipse的workspace是D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\
    然后访问那个java的地址输入的http://localhost:8080/servlet/FirstServlet
      

  4.   

    我建的时候是把默认的package加了个servlets~
    加到哪?~~tomcat启动了~~http://localhost:8080/没问题的~~
    myeclipse的workspace是D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\
    然后访问那个java的地址输入的http://localhost:8080/servlet/FirstServlet
      

  5.   


    myeclipse的workspace是D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\
    然后访问那个java的地址输入的http://localhost:8080/servlet/FirstServlet
      

  6.   

    启动的时候,有没有错误提示信息,贴出来看一下。是不是因为
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>FirstServlet</servlet-class>
    名字和类名一样,就报错了?你换个name试试看。
      

  7.   

    你在D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\
    下建立一个目录 FirstServlet然后把你的项目WebRoot中的所有东西拷到目录 FirstServlet中 ,启动tomcat
    IE地址栏: http://localhost:8080/FirstServlet看能出来欢迎页面不 ?
    如果能出来 
    在IE地址栏: http://localhost:8080/FirstServlet/servlet/FirstServlet看能出来你的结果不 ?你直接把workspace放到webapps下,没用的 如果经常调试修改 ,建议使用一下方法:
    你可以修改tomcat/conf/server.xml
    </Host>前加入:
    <Context debug="0" docBase="D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\项目目录\WebRoot” path="/FirstServlet" reloadable="true" >
    </Context>
          </Host>启动tomcat
    IE地址栏: http://localhost:8080/FirstServlet看能出来欢迎页面不 ?
    如果能出来 
    在IE地址栏: http://localhost:8080/FirstServlet/servlet/FirstServlet看能出来你的结果不 ?
      

  8.   

    估计是因为没有定位到webroot的原因
      

  9.   


    怕是因为名字就重新建了个~~
    web.xml改成了
    <?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">
        <servlet>
        <servlet-name>fs</servlet-name>
        <servlet-class>servlets.firstservlet</servlet-class>
      </servlet>  <servlet-mapping>
        <servlet-name>fs</servlet-name>
        <url-pattern>/servlet/fs</url-pattern>
      </servlet-mapping>
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>启动时候的内容2012-3-20 17:29:23 org.apache.catalina.core.AprLifecycleListener init
    信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Program Files\Local\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin;D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\bin
    2012-3-20 17:29:23 org.apache.coyote.http11.Http11Protocol init
    信息: Initializing Coyote HTTP/1.1 on http-8080
    2012-3-20 17:29:23 org.apache.catalina.startup.Catalina load
    信息: Initialization processed in 445 ms
    2012-3-20 17:29:23 org.apache.catalina.users.MemoryUserDatabase open
    警告: Exception configuring digester to permit java encoding names in XML files. Only IANA encoding names will be supported.
    org.xml.sax.SAXNotRecognizedException: http://apache.org/xml/features/allow-java-encodings
    at gnu.xml.aelfred2.JAXPFactory.setFeature(JAXPFactory.java:102)
    at org.apache.tomcat.util.digester.Digester.setFeature(Digester.java:556)
    at org.apache.catalina.users.MemoryUserDatabase.open(MemoryUserDatabase.java:391)
    at org.apache.catalina.users.MemoryUserDatabaseFactory.getObjectInstance(MemoryUserDatabaseFactory.java:103)
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:140)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:113)
    at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:71)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:137)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:109)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:81)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:747)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    2012-3-20 17:29:23 org.apache.catalina.core.StandardService start
    信息: Starting service Catalina
    2012-3-20 17:29:23 org.apache.catalina.core.StandardEngine start
    信息: Starting Servlet Engine: Apache Tomcat/6.0.33
    2012-3-20 17:29:23 org.apache.catalina.startup.HostConfig deployDescriptor
    信息: Deploying configuration descriptor host-manager.xml
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDescriptor
    信息: Deploying configuration descriptor manager.xml
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory .metadata
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory ca
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory ch08
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory docs
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory examples
    2012-3-20 17:29:24 org.apache.catalina.core.ApplicationContext log
    信息: ContextListener: contextInitialized()
    2012-3-20 17:29:24 org.apache.catalina.core.ApplicationContext log
    信息: SessionListener: contextInitialized()
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory javaweb01
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory JavaWeb02
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory MyProject
    2012-3-20 17:29:24 org.apache.catalina.loader.WebappClassLoader validateJarFile
    信息: validateJarFile(D:\apache-tomcat-6.0.33\apache-tomcat-6.0.33\webapps\MyProject\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
    2012-3-20 17:29:24 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory ROOT
    2012-3-20 17:29:25 org.apache.coyote.http11.Http11Protocol start
    信息: Starting Coyote HTTP/1.1 on http-8080
    2012-3-20 17:29:25 org.apache.jk.common.ChannelSocket init
    信息: JK: ajp13 listening on /0.0.0.0:8009
    2012-3-20 17:29:25 org.apache.jk.server.JkMain start
    信息: Jk running ID=0 time=0/24  config=null
    2012-3-20 17:29:25 org.apache.catalina.startup.Catalina start
    信息: Server startup in 1349 ms
      

  10.   


    这样是可以的~~~那我要怎么改我的project呢?~
      

  11.   

    那要怎么定位到webroot呢?~
      

  12.   

    这是我的一个示例。
    <servlet>
        <servlet-name>ResponseDemo2</servlet-name>
        <servlet-class>cn.haha.response.ResponseDemo2</servlet-class>
    </servlet>
      

  13.   

    看样子好象没有加工程名
    http://localhost:8080/工程名/servlet名
      

  14.   

    type Status reportmessage /ch08/servlet/fsdescription The requested resource (/ch08/servlet/fs) is not available.我把WEB-INFO文件夹从webroot中挪出来就解决了可是如果不像挪的话要怎么解决呢?~
      

  15.   

    报错信息:
    type Status reportmessage /ch08/servlet/fsdescription The requested resource (/ch08/servlet/fs) is not available.我把WEB-INFO文件夹从webroot中挪出来就解决了可是如果不像挪的话要怎么解决呢?~
      

  16.   


    你这样配置,就应该/servlet/FirstServlet这样访问啊,/ch08/servlet/fs都没配,要不你把/servlet/FirstServlet换成/ch08/servlet/fs就行了。
    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/servlet/FirstServlet</url-pattern>
      </servlet-mapping>