Reputation: 189
This is the code to my servlet...
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
private String message;
public void init(){
message="Hello World";
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>"+message+"</h1>");
}
public void destroy(){
}
}
I'm using xampp's tomcat 7
and this is my web.xml file
<web-app 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"
version="3.0"
metadata-complete="true">
<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>
My web.xml is in %TOMCAT_HOME%/webapps/ROOT/WEB_INF directory and my HelloWorld.class is in %TOMCAT_HOME%/webapps/ROOT/WEB_INF/classes directory.
when I try to run my file from my browser I type
http://localhost:8080/HelloWorld
in the addressbar and the following Servlet exception shows up
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class HelloWorld
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)
root cause
java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/HelloWorld/HelloWorld)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:752)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2820)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1150)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.21 logs.
Please suggest a method to run my app properly...
Upvotes: 0
Views: 7076
Reputation: 804
I also faced the same issue. I resolved the issue as follows. IDE Eclipse Photon
Go to this path: Build Path> Configure Build Path > Source
Change the path of "Default output folder": [project name]/WebContent/WEB-INF/classes
Try with this, Sometime it won't work.
If it is not working, create a new Java program with Public static void main(String[] args)
and Run that project as Java Application, so your project will update and a new .class file will be created:
Upvotes: -1
Reputation: 2189
i don't know why you are just typing the servlet mapping url to invoke your web application as follows. http://localhost:8080/HelloWorld
it should be in the following format
http://localhost:8080/your-web-application-name/url-pattern
for example, assume that you are invoking HelloWorld url mapping on Web Application called MyWebApp . it should be called as below.
host:port/MyWebApp/HelloWorld
Upvotes: 0
Reputation: 13566
Default packages are discouraged, always put your servlets and classes in some package. Lets say you have package com.practice
and HelloWorld
servlet in it then your web.xml
becomes
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.practice.HelloWorld</servlet-class>
</servlet>
If you are defining servlet in com.practice
package i.e. path should be WEB-INF/classes/com/practice/HelloWorld
Also, if you are using tomcat 7, then you do not have to use servlet mapping in web.xml at all. You can simply use annotations for the same. e.g.
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private String message;
public void init() {
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy() {
}
}
Here WebServlet
is the annotation that is equalivalent to defining servlet in web.xml
and "/HelloWorld"
is the url pattern
Upvotes: 0
Reputation: 32086
Don't use the default (empty) package; give it a name instead...
package com.xyz;
...
public class HelloWorld extends HttpServlet
Update web.xml to reflect new package...
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.xyz.HelloWorld</servlet-class>
</servlet>
Make sure the servlet class file resides in...
WEB-INF/classes/com/xyz/HelloWorld.class
Upvotes: 6
Reputation: 2896
follow the following steps
Upvotes: 0