Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet example in the java web application

Tags:

java

maven

tomcat

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:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp

I have put HelloWorld.java in the src.main.resources folder, here is the 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("<html>");
  pw.println("<head><title>Hello World</title></title>");
  pw.println("<body>");
  pw.println("<h1>Hello World</h1>");
  pw.println("</body></html>");
  }
}

my web.xml from src.main.webapp.WEB-INF looks like:

!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

here is my pom.xml:

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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
            <url>http://localhost:8080/manager/html</url>
            <server>mytomcat</server>
            <path>/my-app</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Then i type mvn package tomcat:deploy and on the link localhost:8080/web/HelloWorld -

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)
like image 955
fen1ksss Avatar asked Oct 06 '22 13:10

fen1ksss


1 Answers

I have put HelloWorld.java in the src.main.resources folder

The java code should not be in src/main/resources, it should be in src/main/java.

Your HelloWorld.java has never been built and thus it cannot be found by tomcat. Your war file does not contain the HelloWorld.class.

like image 193
maba Avatar answered Oct 10 '22 02:10

maba