求教一个简单的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 ");
Dogd = 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 ">
<servlet>
<servlet-name> ListenerTester </servlet-name>
<serlet-class> example.ListenerTester </serlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ListenerTester </servlet-name>
<url-pattern> /ListenTest.do </url-pattern>
</servlet-mapping>
<context-param>
<param-name> breed </param-name>
<param-value> Great Dane </param-value>
</context-param>
<listener>
<listener-class> example.MyServletContextListener </listener-class>
</listener>
</web-app>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
访问路径是http://localhost:8080/ListenTest.do
[解决办法]
你看tomcat的webbhapps目录下有没有你的目录,然后再在配置文件里配置1吓welcome的文件
[解决办法]
<serlet-class> example.ListenerTester </serlet-class> 不对啊
应该时 <servlet-class> example.ListenerTester </servlet-class>