2

I have created a sample maven project using the following command:

mvn archetype:generate -DgroupId=com.rohit -DartifactId=RohitLoginApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Below is the image link of the project structure in eclipse:

Maven project structure in eclipse

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.rohit</groupId>
  <artifactId>RohitLoginApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RohitLoginApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>   
  </dependencies>
  <build>
    <finalName>RohitLoginApp</finalName>
  </build>
</project>

web.xml:

<!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>RohitTestServlet</servlet-name>
    <display-name>RohitTestServlet</display-name>
    <description></description>
    <servlet-class>com.company.rohit.RohitTestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RohitTestServlet</servlet-name>
    <url-pattern>/RohitTestServlet</url-pattern>
  </servlet-mapping>
</web-app>

RohitTestServlet.java

package com.company.rohit;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RohitTestServlet
 */
public class RohitTestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RohitTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String userId = request.getParametker("userName");
        String pwd = request.getParametker("psw");
        response.getWriter().write("User ID and password are received");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Index.jsp

<html>
<body>
<h2>Login Page</h2>
<form action="RohitTestServlet">
  User Name<br>
  <input type="text" name="userName">
  <br>
  Password<br>
  <input type="password" name="psw">
  <br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

When I deploy this app on tomcat, it first loads the welcome screen where I enter the userID and password and click on submit button. Then it redirects to servlet url i.e. http://localhost:8080/RohitLoginApp/RohitTestServlet?userName="xyz"&psw="www". However its unable to hit the servlet and get the response. Below is the error:

SEVERE: Allocate exception for servlet RohitTestServlet java.lang.ClassNotFoundException: com.company.rohit.RohitTestServlet at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

It is unable to load the class. I had done clean build lot of times but still its unable to load the servlet.

RRN
  • 61
  • 1
  • 5
  • 13
  • You have a typo in `request.getParametker()` – Krzysiek Nov 22 '16 at 22:08
  • Have you tried right click on project in Package Explorer and select Maven -> Update Project (Alt + F5) ? Then go to Servers tab, unfold Tomcat instance and delete your project from there. After that hit Run as.. -> Run on server – Krzysiek Nov 22 '16 at 22:17

1 Answers1

1

It looks like your class is not being compiled into the war file. You need to make sure your servlet class is defined in src/main/java with the appropriate package subfolders. You can verify this by going to webapps directory and looking in the "classes" folder to see if your class is there.

GameSalutes
  • 1,762
  • 2
  • 18
  • 24
  • Thanks for the response. What you said is rite. When I gave the maven archtype as "maven-archetype-webapp", I thought maven will create the appropriate folder structure. Is that not true ? – RRN Nov 22 '16 at 22:18
  • From your image you defined your servlet class in "src/main/resources" instead of "src/main/java". "src/main/resources" is for static resources such as properties files. All code needs to go under "src/main/java" to be compiled by the java compiler when building the war. – GameSalutes Nov 22 '16 at 22:21
  • ok. But I dont have any folder i.e. src/main/java in my project. So do I need to create one ? – RRN Nov 22 '16 at 22:24
  • 1
    Right click on project and click "New" -> "Source Folder" – GameSalutes Nov 22 '16 at 22:29
  • Thanks it worked now. So one last question everytime we create a maven project, it wont create the src/main/java folder automatically. Do we need to create those folders manually ? – RRN Nov 22 '16 at 22:48
  • Depending on version of Eclipse it may be creating it automatically as maven itself creates it but just hiding it from the build path since there is initially no files in it. I agree that this is not optimal. – GameSalutes Nov 22 '16 at 22:58