views:

207

answers:

3

I seem to be having trouble with Tomcat servlets and even setting up my own. I am using Windows Vista x64 bit. I CAN connect and see the servlet examples. I just don't know how to go about creating my own.

Can anyone pinpoint to me a guide on "my first servlet" with a step-by-step instructions on how-to?

Thanks.

Anyone?

+7  A: 
  1. Create the servlet class:

    package com.example;
    import javax.servlet.http.HttpServlet;
    public MyServlet extends HttpServlet {}
    
  2. Override the doGet(..) method in your class:

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
         response.getWriter().print("Hello world");
    }
    
  3. Put your servlet class in WEB-INF/classes/com/example/MyServlet.class

  4. Go to (or create) your web.xml (like this) in the WEB-INF/ folder

  5. Map your servlet in web.xml:

    <servlet>
        <servlet-name>myServletName</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
    </servlet>
    
    
    <servlet-mapping>
       <servlet-name>myServletName</servlet-name>
       <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>
    
  6. Access your servlet using http://localhost:8080/myApplication/myServlet

Bozho
Where would I put "MyServlet.java"... inside ROOT/myApplicaton?
Dan
@Dan updated my answer with it (p3)
Bozho
@Dan: Also, look at `$CATALINA_HOME/webapps/examples/WEB-INF/web.xml` to see how the classes in `$CATALINA_HOME/webapps/examples/WEB-INF/classes` are mapped (step 5) and accessed from `$CATALINA_HOME/webapps/examples/servlets/index.html` (step 6).
trashgod
I am guessing I create the classes, com, and example folders?
Dan
Also... package com.example;import javax.servlet.http.HttpServlet;public MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { response.getWriter().print("Hello world"); }}That is not working...
Dan
I'm still get 404... pages. Can you be more descriptive?
Dan
HTTP Status 404 - /myServlet/myServlet WHATTTTThttp://localhost:8080/myServlet/myServlet ...help?
Dan
It should be `http://localhost:8080/contextname/myServlet` where `contextname` is the context name of the webapplication. This is usually the same as folder name in `Tomcat/webapps`, else it is definied in webapp's `context.xml`.
BalusC
+2  A: 

The Development Processes section of the Tomcat 6.0 Application Developers Guide is a step by step guide that covers creating your sandbox, adding content, building a WAR file and deploying it. Another section gives you a pre-built WAR file that you can use an example to help you get started.

Stephen C
A: 

If you area beginner to Java Web developement, you might consider using some IDE that can support your development process, deploy and debug your application and support additional frameworks you might use in the feature (JSF, Spring MVC, Dojo etc.)

http://www.google.com/search?hl=en&amp;safe=off&amp;client=firefox-a&amp;rls=org.mozilla%3Aen-GB%3Aofficial&amp;hs=dUq&amp;num=20&amp;q=netbeans+tomcat+howto&amp;lr=&amp;aq=f&amp;aqi=&amp;oq=

http://www.google.com/search?hl=en&amp;safe=off&amp;client=firefox-a&amp;rls=org.mozilla%3Aen-GB%3Aofficial&amp;hs=iUq&amp;num=20&amp;q=eclipse+tomcat+howto&amp;lr=&amp;aq=f&amp;aqi=g-c1&amp;oq=

Deploying with these is a single click. It is really hard to find a reason not using an IDE here.

sibidiba
If you looked closed. You'd noticed that I cannot use Eclipse JEE version because I'm on a x64 bit....
Dan
@Dan Oh? Why not? You can either get Eclipse 32 bit and run it on a 32 bit VM or get Eclipse Java IDE 64 bit (http://download.eclipse.org/eclipse/downloads/drops/R-3.5-200906111540/winPlatform.php#EclipseSDK) and install the WTP on top of it.
Pascal Thivent
+1 for Pascal. Yeas You Can! 64 bit isn't a problem in Javaland.
sibidiba
What does WTP mean... you linked me to SDK and not the Eclipse Java IDE 84-bit...
Dan