求教一个简单的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= &