0

我尝试从 Tomcat 7 开始。
我在 Eclipse 中创建了应用程序。这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

<welcome-file-list>
 <welcome-file>
  view.jsp
 </welcome-file>
</welcome-file-list>

<servlet>
 <servlet-name>myServlet</servlet-name>
 <servlet-class>/servlets/myServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>myServlet</servlet-name>
 <url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>

我从 Apache 的站点下载了最新的 Tomcat,并添加JAVA_HOMEcatalina.bat. 启动 Tomcat 后,我​​进入Manager app并选择了我的应用程序,但得到了 404。在地址行中 - http://localhost:8080/ThreeRest/
另一个奇怪的事情是应用程序没有部署到webapps目录中,而是部署到wtpwebapps文件夹中。

我的另一个问题tomcat-users.xml。如果我添加这个:

<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="admin,manager,manager-gui"/>

它只在一个会话中工作。当我停止 tomcat 时,它会从文件中删除。

4

2 回答 2

2

<servlet-class>应该

<servlet-class>servlets.myServlet</servlet-class>

因为您在此处指定了一个包而不是路径。

请注意,您必须通过以下任一方式访问您的网站

http://localhost:8080/ThreeRest/myServlet

或者

http://localhost:8080/ThreeRest/

view.jsp您的网络应用程序的根文件夹中。

EDIT: Once deployed your web application's folder structure should be like: (/ indicates a directory)

tomcat-home/
 |- webapps/
   |- rest/ //<-- Context-Root (Web-app's name)
     |- view.jsp //<-- *.html, *.jsp files
     |- WEB-INF/
        |- web.xml
        |- lib/
          |- *.jar files
        |- classes/ //<-- ALL your servlets go here
          |- servlets/ //<-- with the required package/folder structure
            |- myServlet.class
于 2013-05-20T04:42:12.160 回答
1

Ok, a sample config, for servlet declaration:

Let's assume you are creating a servlet (HelloServlet which is in package x.y.z):

So code is something like:

package x.y.z;

//imports here

public class HelloServlet extends HttpServlet {

....Code here

}

Now in web.xml if I want to map this servlet I will do something like:

 <servlet>
     <servlet-name>myservlet</servlet-name>
     <servlet-class>
           x.y.z.HelloServlet
     </servlet-class>
 </servlet>

 <servlet-mapping>
     <servlet-name>myservlet</servlet-name>
     <url-pattern>/myservlet</url-pattern>
 </servlet-mapping>

This is suffice, once app is deployed in tomcat, say the context name is testservlet , then I can access this servlet like:

 http://<ip>:<port on which tomcat is running>/testservlet/myservlet
于 2013-05-20T05:03:55.003 回答