Issue
I am trying to create my 1st servlet program but while running my servlet I am getting internal server 500 error saying Error instantiating servlet class and
Exception:-
javax.servlet.ServletException: Error instantiating servlet class [com.servlets.MyFirstServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
Root cause:-
java.lang.RuntimeException:
com.servlets.MyFirstServlet.<init>(MyFirstServlet.java:1)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
is there any environment variable setup required? if so pls guide me on how to do that. Here is my directory.
and here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--servlet defn-->
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>com.servlets.MyFirstServlet</servlet-class>
</servlet>
<!--servlet mapping-->
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/web</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
and my servlet class:-
package com.servlets;
import java.io.IOException;
import javax.servlet.*;
public class MyFirstServlet implements Servlet {
ServletConfig conf;
@Override
public void init(ServletConfig conf) throws ServletException {
this.conf = conf;
System.out.println("Creating servlet object ...Inside init method");
}
@Override
public ServletConfig getServletConfig() {
return this.conf;
}
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
System.out.println("Inside Service Method...");
}
@Override
public String getServletInfo() {
return "This servlet is created by Ashish Raj...";
}
@Override
public void destroy() {
System.out.println("Inside Destroy method...going to destroy servlet");
}
}
Pls co-operate with me I m new to this... :)
Solution
Ultimately Netbeans doesn't know how to build this correctly. It's trying to run your Java file directly and it can't do that.
Can I recommend a simpler start? I'm not sure where you got that servlet code but it's highly likely not what you want. You need two files, placed in very specific directories.
First at the root of your development area for this project you'll need a file named pom.xml
. It contains:
<?xml version="1.0" encoding="UTF-8"?>
<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.example.servlet</groupId>
<artifactId>servlet-example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Again, this is at the root of your project. Next, you need your servlet. This lives in src/main/java/com/example/servlet
and has:
package com.example.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "MyFirstServlet", urlPatterns = "/web")
public class MyFirstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("in doPost()");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("in doGet()");
}
}
Lastly, to help your IDE you should create the directory src/main/webapp/WEB-INF
. There will be nothing in there but it tells Netbeans that this is a regular web-app.
So your entire directory will look like:
.
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ └── servlet
│ └── MyFirstServlet.java
└── webapp
└── WEB-INF
Import this into Netbeans. It is a Maven project if Netbeans asks.
When you run this it will be in a web-app. So, for example, when I ran it Tomcat put it into the servlet-example-1.0.0-SNAPSHOT
webapp. That means that to access the services I had to go to http://localhost:8080/servlet-example-1.0.0-SNAPSHOT/web
. The /web
part is defined in your servlet in the urlPatterns
parameter. Note that the servlet-example-1.0.0-SNAPSHOT
part may be different depending on how Netbeans builds it for you.
Answered By - stdunbar