我基于这个例子创建了一个 webapp:
http://code4reference.com/2012/09/hello-world-web-application/
我已使用 eclipse 4.2 将其转换为动态 Web 项目。结构是:
MyProject
-> src
-> webapp
-> WEB-INF
-> appContext
-> beans.xml
-> mappings.xml
-> web.xml
我在 eclipse 中设置服务器时指定了本地安装的 tomcat 7。当我运行服务器(使用上面的网络应用程序)时,我得到:
14-11-2012 14:12:08 org.apache.catalina.core.AprLifecycleListener init
...
14-11-2012 14:12:09 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyProject' did not find a matching property.
14-11-2012 14:12:09 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
14-11-2012 14:12:09 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
14-11-2012 14:12:09 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 563 ms
14-11-2012 14:12:09 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
14-11-2012 14:12:09 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.32
14-11-2012 14:12:09 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
14-11-2012 14:12:09 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
14-11-2012 14:12:09 org.apache.catalina.startup.Catalina start
INFO: Server startup in 680 ms
然后什么也没有发生。我试图访问这个网址:
http://localhost:8080/HelloWorld/hi
但获得 HTTP 状态 404 - /HelloWorld/hi。是否有任何日志可以将我指向出错的方向?
编辑:我也尝试遵循本指南:
http://www.eclipse.org/webtools/community/tutorials/BuildJ2EEWebApp/BuildJ2EEWebApp.html
但给出相同的结果 - 没有任何反应。我试图在该行中放置一个断点:
String userAgent = req.getHeader("user-agent");
在:
public class SnoopServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String userAgent = req.getHeader("user-agent");
但它永远不会到达那条线。如果我在 Eclipse 中右键单击项目(MyWebApp)并选择“运行方式 -> 在服务器上运行”,则会在 Eclipse 中打开浏览器:
项目添加到服务器:
并在 Eclipse 中运行:
这是 com.sample.SnoopServlet 的内容:
package com.sample;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SnoopServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String userAgent = req.getHeader("user-agent");
String clientBrowser = "Not known!";
if( userAgent != null)
clientBrowser = userAgent;
req.setAttribute("client.browser",clientBrowser );
req.getRequestDispatcher("/showBrowser.jsp").forward(req,resp);
}
}
这是 web.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<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>Snoop Servlet</servlet-name>
<servlet-class>com.sample.SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Snoop Servlet</servlet-name>
<url-pattern>/snoop/*</url-pattern>
</servlet-mapping>
<!-- Don't remove the below section -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>