title: Servlet example in the java web application
body: <p>I'm new to java web application development, can you tell me, what am I doing wrong:
  I have generated the web application using maven:</p>
  
  <pre><code>mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp
  </code></pre>
  
  <p>I have put <strong>HelloWorld.java</strong> in the src.main.resources folder, here is the code:</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class HelloWorld extends HttpServlet{ 
    public void doGet(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException,IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("&lt;html&gt;");
    pw.println("&lt;head&gt;&lt;title&gt;Hello World&lt;/title&gt;&lt;/title&gt;");
    pw.println("&lt;body&gt;");
    pw.println("&lt;h1&gt;Hello World&lt;/h1&gt;");
    pw.println("&lt;/body&gt;&lt;/html&gt;");
    }
  }
  </code></pre>
  
  <p>my <strong>web.xml</strong> from src.main.webapp.WEB-INF looks like:</p>
  
  <pre><code>!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  
  &lt;web-app&gt;
      &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Hello&lt;/servlet-name&gt;
          &lt;servlet-class&gt;HelloWorld&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Hello&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/HelloWorld&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>here is my <strong>pom.xml</strong>:</p>
  
  <pre><code>project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.mycompany.app&lt;/groupId&gt;
    &lt;artifactId&gt;my-webapp&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;my-webapp Maven Webapp&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
      &lt;finalName&gt;my-webapp&lt;/finalName&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
          &lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;
          &lt;configuration&gt;
              &lt;url&gt;http://localhost:8080/manager/html&lt;/url&gt;
              &lt;server&gt;mytomcat&lt;/server&gt;
              &lt;path&gt;/my-app&lt;/path&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Then i type 
  <code>mvn package tomcat:deploy</code> 
  and on the link localhost:8080/web/HelloWorld - </p>
  
  <pre><code>HTTP Status 500 - Error instantiating servlet class HelloWorld
  
  type Exception report
  
  message Error instantiating servlet class HelloWorld
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: Error instantiating servlet class HelloWorld    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)     org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)     org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)     java.lang.Thread.run(Thread.java:680) root cause
  
  java.lang.ClassNotFoundException: HelloWorld    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)     org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)     org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)     org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)     java.lang.Thread.run(Thread.java:680)
  </code></pre>