Kliver Max
Kliver Max

Reputation: 5299

Can't deploy application to Tomcat

I tried to start with Tomcat 7.
I created the application in Eclipse. Here is my web.xml file:

<?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>

I downloaded the lastest Tomcat from Apache's site, and added JAVA_HOME to catalina.bat. After starting Tomcat I went in Manager app and chose my application but got 404. In the address line - http://localhost:8080/ThreeRest/.
Another strange thing is that the application didn't deploy into webapps directory but into wtpwebapps folder.

My other problem with tomcat-users.xml. If I add this:

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

Its work only in one session. When I stop tomcat it is removed from file.

Upvotes: 0

Views: 4536

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

<servlet-class> should be

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

because you specify a package here not a path.

Please note that you must access your website at either

http://localhost:8080/ThreeRest/myServlet

or

http://localhost:8080/ThreeRest/

with view.jsp at your web-app's root folder.

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

Upvotes: 2

Himanshu Bhardwaj
Himanshu Bhardwaj

Reputation: 4123

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

Upvotes: 1

Related Questions