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.

12
  • Are you deploying the application directly from eclipse? Commented May 20, 2013 at 4:35
  • @Himanshu Bhardwaj: Yeah. I click Run on Server in Run As menu. Commented May 20, 2013 at 4:36
  • stackoverflow.com/questions/3515089/… this will resolve few queries. Commented May 20, 2013 at 4:44
  • @Himanshu Bhardwaj: I already have cheacked radio button like in Pascal Thivent answer. But this radiobutton group desabled anyway. Commented May 20, 2013 at 4:46
  • Let's debug one step at a time. If you manually move your web-app into webapps folder are you able to access it? (Try removing auth related stuff for now.) Commented May 20, 2013 at 4:48

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.