1

I am trying to run a simple servlet in maven using eclipse. But getting error 500. Please see what I am doing wrong. Main concern is how to run a servlet in maven as I am not familiar with maven .

index.jsp

  <html>
  <body>
  <h2>Hello World!</h2>
  <form action="mylog" method="post">  
  loginID:<input type="text" name="name"/><br/>
  <input type="submit" value="login"/> 

  </form>
  </body>
  </html>

mylog.java (servlet class)

  import java.io.IOException;
  import java.io.PrintWriter;

  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;


   public class mylog extends HttpServlet {
   private static final long serialVersionUID = 1L;


   public mylog() {
    super();
    // TODO Auto-generated constructor stub
    }


     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
     }


     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
      String n = request.getParameter("name");

      PrintWriter out = response.getWriter();
      out.print(n);
      out.close();

      doGet(request, response);
    }

}

web.xml and directory structure- see attached image enter image description here

error stack

     HTTP Status 500 - Error instantiating servlet class com.issac.mylog

  type Exception report

  message Error instantiating servlet class com.issac.mylog

 description The server encountered an internal error that prevented it from fulfilling this request.

 exception

javax.servlet.ServletException: Error instantiating servlet class     com.issac.mylog
  org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
  org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
  org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
  org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
  org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
   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)

root cause

 java.lang.ClassNotFoundException: com.issac.mylog
  org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
  org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
  org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
  org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
  org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
  org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
  org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    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)
techvigil
  • 47
  • 1
  • 8

2 Answers2

2

src/main/resources is for placing properties files (or static resources), So the server could NOT find class files, so you need to create the Java source files under src/main/java folder.

Also, make sure that the .class files are generated and available in the Server classpath (i.e., under WEB-INF/classes), which will resolve the above java.lang.ClassNotFoundException

Vasu
  • 21,832
  • 11
  • 51
  • 67
1

Create a source folder src/main/java and move the java source file there

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • tried creating it but shows "this folder is already a source folder" , also when I go to build path->order and export, it shows all three src/main( resources, java & test) but with "missing" written in bracket for src/main/java& src/main/test. Also I was trying a similar project two days ago there it showed all three, and servlet was workin too. – techvigil Nov 06 '16 at 05:36
  • Sounds like Eclipse is out of sync with the file system. Try deleting the project in Package Explorer (view only - not files), then re-import as a Maven project – Reimeus Nov 06 '16 at 11:10
  • yes, there was some issue with eclipse that it didn't show all three folders. But now that I've tried two diff projects it works fine for one but same problem for other project. Isn't there a permanent solution for this. – techvigil Nov 06 '16 at 13:29
  • There can be a number of different causes. See [this post](http://stackoverflow.com/questions/18790106/eclipsemaven-src-main-java-not-visible-in-src-folder-in-package-explorer). One surefire way to ensure everything shows up is to create the project using a Maven archetype and import – Reimeus Nov 06 '16 at 13:39
  • the error 500 persists for a different project with similar servlet even after placing under specified folder. – techvigil Nov 06 '16 at 13:49