[英]I'm having problems with my first servlet
This is the code to my servlet... 这是我的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 我正在使用xampp的tomcat 7
and this is my web.xml file 这是我的web.xml文件
<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. 我的web.xml位于%TOMCAT_HOME%/ webapps / ROOT / WEB_INF目录中,而我的HelloWorld.class位于%TOMCAT_HOME%/ webapps / ROOT / WEB_INF / classes目录中。
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 在地址栏中,并显示以下Servlet异常
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... 请提出一种正确运行我的应用的方法...
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... 更新web.xml以反映新软件包...
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.xyz.HelloWorld</servlet-class>
</servlet>
Make sure the servlet class file resides in... 确保servlet类文件位于...
WEB-INF/classes/com/xyz/HelloWorld.class
follow the following steps 遵循以下步骤
Default packages are discouraged, always put your servlets and classes in some package. 不鼓励使用默认软件包,请始终将servlet和类放在某个软件包中。 Lets say you have package com.practice
and HelloWorld
servlet in it then your web.xml
becomes 假设您有com.practice
包和HelloWorld
servlet包,然后您的web.xml
变为
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.practice.HelloWorld</servlet-class>
</servlet>
If you are defining servlet in com.practice
package ie path should be WEB-INF/classes/com/practice/HelloWorld
如果要在com.practice
包中定义servlet,即路径应为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. 另外,如果您使用的是tomcat 7,则根本不必在web.xml中使用servlet映射。 You can simply use annotations for the same. 您可以简单地使用注释。 eg 例如
@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 这里的WebServlet
是与在web.xml
定义servlet WebServlet
的注释, "/HelloWorld"
是url模式
i don't know why you are just typing the servlet mapping url to invoke your web application as follows. 我不知道您为什么只按如下所示输入servlet映射URL来调用您的Web应用程序。 http://localhost:8080/HelloWorld HTTP://本地主机:8080 /的HelloWorld
it should be in the following format 它应采用以下格式
http://localhost:8080/your-web-application-name/url-pattern HTTP://本地主机:8080 /你的web应用程序的名称/ URL模式
for example, assume that you are invoking HelloWorld url mapping on Web Application called MyWebApp . 例如,假设您正在调用Web应用程序MyWebApp上的HelloWorld url映射。 it should be called as below. 它应该被称为如下。
host:port/MyWebApp/HelloWorld 主机:端口/ MyWebApp /的HelloWorld
I also faced the same issue. 我也面临同样的问题。 I resolved the issue as follows. 我解决了以下问题。 IDE Eclipse Photon IDE Eclipse光子
Go to this path: Build Path> Configure Build Path > Source 转到此路径:构建路径>配置构建路径>源
Change the path of "Default output folder": [project name]/WebContent/WEB-INF/classes 更改“默认输出文件夹”的路径:[项目名称] / 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: 如果它不起作用,请使用Public static void main(String[] args)
创建一个新的Java程序,然后将该项目作为Java Application运行,这样您的项目将更新并创建一个新的.class文件:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.