MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 求教一个简单的tomcat下访问servlet有关问题

求教一个简单的tomcat下访问servlet有关问题

www.MyException.Cn  网友分享于:2013-12-29  浏览:15次
求教一个简单的tomcat下访问servlet问题
请大家帮忙看下好吗,我是做delphi的,因为兴趣原因,现在想学习java   web开发。
最近买了本head   first   servlet&jsp,按照书中讲解上下文监听器的例子做了一遍,怎么都调试不正确,每次都报HTTP   Status   404   -   Servlet   ListenerTester   is   not   available。
例子程序共三个类,如下
Dog.java
-----------------------------
public   class   Dog
{
private   String   breed;

public   Dog(String   breed)
{
this.breed   =   breed;
}

public   String   getBreed()
{
return   breed;
}

}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ListenerTester.java
--------------------------
import   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   ListenerTester   extends   HttpServlet
{


public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)
throws   ServletException,   IOException
{

response.setContentType( "text/html ");
PrintWriter   out   =   response.getWriter();
Dog   dog   =   (Dog)   getServletContext().getAttribute( "dog ");
out.println( "test   listener "   +   dog.getBreed());
out.flush();
out.close();
}

}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


MyServletContextListener.java
-------------------------------
import   javax.servlet.ServletContext;
import   javax.servlet.ServletContextEvent;
import   javax.servlet.ServletContextListener;


public   class   MyServletContextListener   implements   ServletContextListener
{


/*   (非   Javadoc)
  *   @see   javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  */
public   void   contextDestroyed(ServletContextEvent   event)
{
//   TODO   自动生成方法存根

}

/*   (非   Javadoc)
  *   @see   javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  */
public   void   contextInitialized(ServletContextEvent   event)
{
//   TODO   自动生成方法存根
ServletContext   sc   =   event.getServletContext();
String   dogBreed   =   sc.getInitParameter( "breed ");
Dog d   =   new   Dog(dogBreed);
sc.setAttribute( "dog ",   d);
}

}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


目录结构
三个类都放在E:\tomcat\webapps\ROOT\WEB-INF\classes目录下
web.xml放在E:\tomcat\webapps\ROOT\WEB-INF目录下


web.xml
----------------------
<?xml   version= "1.0 "   encoding= "ISO-8859-1 "?>
<web-app   xmlns= "http://java.sun.com/xml/ns/j2ee "   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "   xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "   version= "2.4 ">

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有