日期:2014-05-17  浏览次数:20680 次

Tomcat虚拟目录下编写servlet有问?
在E盘建一个tomcat虚拟目录,在web-inf里建一个classes文件夹,在里面编写HelloServlet.java文件
Java code
package org.yj.ServletDemo1;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

class HelloServlet extends HttpServlet
{
    public  void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     java.io.IOException{
       
       PrintWriter out=resp.getWriter();

       out.println("<html>");
       out.println("<title>");
       out.println("mldnJAVA");
       out.println("</title>");
       out.println("<body>");
       out.println("</body>");
       out.println("</html>");
       
       out.close();

    }
}


这段代码编译通过了,然后我配置web-inf下web.xml。
XML code
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<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_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
[color=#FF0000]
  <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.yj.ServletDemo1.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/helloServlet</url-pattern>
  </servlet-mapping>[/color]

</web-app>


奇怪的是,这时候启动tomcat就出错误,而且还直接关闭,根本看不到什么错误。
如果不配置xml就可以启动正常
这配置有问题?

------解决方案--------------------
web.xml中没有启动页面,即welcome-file,应该还没有调用你的HelloServlet,指定一个能正常显示的jsp做welcome-file试试
------解决方案--------------------
顾名思义,classes目录是放class类文件的,你放java文件木有用
------解决方案--------------------
在tomcat安装位置下找找logs看报错