0

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Kliver Max
  • 5,107
  • 22
  • 95
  • 148

2 Answers2

2

<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
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
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
Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
  • Thanks for config. I try to get access on `http://localhost:8080/rest/myServlet/` and get blank page. If i have `view.jsp` as main site page how to get access to it? – Kliver Max May 20 '13 at 05:10
  • @KliverMax You can't have a `/` after `myServlet` and wasn't your app's name `ThreeRest`? Please verify you use the same name at all places i.e. if `rest` the your app's folder under `webapps` should also be `rest`. – Ravi K Thapliyal May 20 '13 at 05:15
  • @Ravi Thapliyal: Now i deploy by WAR files. So in tomcat application name = name of WAR file. I have success. I remove `view.jsp` from `WEB-INF` folder and put it into WebContent directory. I can get access to it on `http://localhost:8080/rest/view.jsp`. – Kliver Max May 20 '13 at 05:25
  • @KliverMax Good. (Had already mentioned about `view.jsp` in my answer.) Now just follow my last two comments on your question to access your `myServlet`. – Ravi K Thapliyal May 20 '13 at 05:30