简体   繁体   中英

Deploying Servlet on Tomcat 8.0.30

I am trying to deploy a compiled Servlet class onto Apache Tomcat server 8.0.30.

But i get the following exception :

javax.servlet.ServletException: Error instantiating servlet class HelloWorld

   java.lang.ClassNotFoundException: HelloWorld

My tomcat webapps/ROOT/ folder did not contain the classes folder so i created one and copied HelloWorld.class into it. I added the following lines in web.xml :

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
<servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>HelloWorld</servlet-class>
 </servlet>

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

Can anyone tell me what am I doing wrong?

@wero : This is the content of my HelloWorld.java :

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {

  private String message;

  public void init() throws ServletException
  {
      // Do required initialization
      message = "Hello World";
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }

  public void destroy()
  {
      // do nothing.
  }
}

When tomcat had been started there were not class file in your app. Tomcat is loading classes during deployment on startup or if you manually deploy it at runtime. After that the context should be reloaded. You can't just copy your classes to the tomcat webapps folder without loading them.

Read Tomcat docs how to deploy your application .

Deployment is the term used for the process of installing a web application (either a 3rd party WAR or your own custom web application) into the Tomcat server.

Web application deployment may be accomplished in a number of ways within the Tomcat server:

  • Statically (the web application is setup before Tomcat is started)
  • Dynamically (by directly manipulating already deployed web applications (relying on auto-deployment feature) or remotely by using the Tomcat Manager web application)

The Tomcat Manager is a web application that can be used interactively (via HTML GUI) or programmatically (via URL-based API) to deploy and manage web applications.

There are a number of ways to perform deployment that rely on the Manager web application. Apache Tomcat provides tasks for Apache Ant build tool. Apache Tomcat Maven Plugin project provides integration with Apache Maven. There is also a tool called the Client Deployer, which can be used from a command line and provides additional functionality such as compiling and validating web applications as well as packaging web application into web application resource (WAR) files.

You need to put the files into the correct places. Java class files need to be placed into WEB-INF/classes:

webapps/ROOT/WEB-INF/web.xml
webapps/ROOT/WEB-INF/classes/HelloWorld.class

Then start Tomcat and look if there are errors in the Tomcat log.

Got it. The classes folder I created was " C "lasses where tomcat expects " c "lasses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM