Hi friends....
I want to execute my first servlet but i am not able. I am going to explain what steps I did.
1-First I copy "Servlet-api.jar" from lib folder in tomcat and paste it in ext folder of java.
2- Create HelloWorld.java in somewhere in my hard drive

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

3-Compile this file and put its .class file in below location
"C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF".

4-Now I edited web.xml file which is give below

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <servlet>
  <servlet-name>Ankit</servlet-name>
  <servlet-class>HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>Ankit</servlet-name>
  <url-pattern>/mainfile</url-pattern>
  </servlet-mapping>
</web-app>

5- Now I try "http://localhost:8080/mainfile" in my browser but I get the following error.

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Wrapper cannot find servlet class HelloWorld or a class it depends on
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
	java.lang.Thread.run(Unknown Source)

root cause

java.lang.ClassNotFoundException: HelloWorld
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
	org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
	org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
	java.lang.Thread.run(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs.

So, friends please tell me where I am wrong or what steps I am missing.
Please help....
Thanks in advance.....

There are a lot of things going wrong here.

First, you should never ever mess around with your Java installation. Never. Setting class-path when compiling your files should be the way to go. Also, don't mess around with the "ROOT" application which comes pre-installed with your Tomcat.

I'll outline the steps in brief as to how you can get your first servlet up and running.

  • First, install Tomcat on your machine
  • Set the environment variable (Right click my computer -> properties -> advanced tab -> environment variables -> New) TOMCAT_HOME to point to your Tomcat installation directory. Click OK.
  • Open up a *new* command prompt and type: echo %TOMCAT_HOME% and it should display the directory of your Tomcat installation. If not, go back to the previous step and try to understand what went wrong.
  • In the "webapps" folder of your tomcat installation, create a new folder called "test". Create a "WEB-INF" directory in the test directory. Navigate to the WEB-INF directory and create two directories inside it; src and classes (normally "src" files are *not* part of WEB-INF but normally are part of your IDE project but we'll keep it simple for now)
  • Create your test servlet Java file and place it into the "test" folder inside the "src" directory. Also, make sure that you have a package declaration at the top of your Java file. In my case, I'll assume the package name of "test".
  • Now, assuming you are in the "src" directory, compile the classes by typing: javac -d ..\classes -cp %TOMCAT_HOME%\lib\servlet-api.jar test\HelloWorld.java
  • The above command will create a "test" directory in the "classes" directory containing the class file for HelloWorld.
  • Create and popuate the web.xml file inside the WEB-INF folder. Also, make sure that the servlet class name is specified as "test.HelloWorld" instead of just HelloWorld.
  • Now, start the Tomcat server (startup.bat), navigate to the location: http://localhost:8080/test/mainfile
  • You should now see the page you desire.
commented: thor·ough +15

Thanks for your great help....
But cmd gives "file not found" on compiling.
Help me sir...

You need to provide more details. Can you paste your console (cmd window) session here?

Hi dude,

Try to use good editor like eclipse, it consume your max time.

1. Right click on project explorer -> create new dynamic project.
2. Right click on your project -> new -> select servlet (if not go other then select)->give your servlet name -> select default pakage-> uncheck dopost method -> finish.
3. write code on doget method,

printWriter out = response.getWriter();
 out.println("hello world");

4. run the application.
5. it ask server to select -> click tomcat 6.0 -> if already install your disk select that drive or move your mouse to corner to install from internet.
6.Run the application
7. Have fun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.