RBhagwat RBhagwat - 2 months ago 19
Java Question

Servlet Mapping and deploying in Tomcat

I am developing a simple servlet based web application. I am not using any IDE. Following are the steps that i have followed.


  1. Folder Structure :


    • MyAPP -> Contains WEB-INF folder and index.html

    • WEB-INF contains 2 folders and web.xml.

    • Path to loginServlet.java : MyAPP\WEB-INF\classes\servletclass\loginServlet.java

    • WEB_INF contains lib folder which conatins servlet-api.jar


  2. index.html contains a form and the action attribute of the form contains "login" as value.

  3. MyAPP\WEB-INF\web.xml looks like below :

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>servletclass.loginServlet</servlet-class>
    </servlet>


    <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
    </servlet-mapping>



    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>

    </web-app>

  4. Compiled the loginServlet.java using command prompt. It created a loginServlet.class file.

  5. Now built a war file using command prompt and deployed it on Tomcat server.

  6. Now when i run the application, it succesfully loads index.html. But as soon as click on Submit button in the form it shows the following error:

    java.lang.NoClassDefFoundError: servletclass/loginServlet (wrong name: loginServlet)
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2260)
    org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:803)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1244)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:456)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1452)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)


Answer

Alan is right, do you have the following declaration in your Java file?

package servletclass;
Comments