简体   繁体   English

我的第一个servlet遇到问题

[英]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 遵循以下步骤

  • Under the webapps folder, create a new folder for your webapp. 在webapps文件夹下,为您的webapp创建一个新文件夹。 HelloWorld 你好,世界
  • Under HelloWorld create another folder called WEB-INF. 在HelloWorld下,创建另一个名为WEB-INF的文件夹。
  • Under WEB-INF, create a folder called classes. 在WEB-INF下,创建一个名为classes的文件夹。
  • under WEB-INF, put your web.xml . 在WEB-INF下,将您的web.xml放入。
  • under the classes folder create your package structure com/HelloWorld 在classes文件夹下,创建您的包结构com / HelloWorld
  • put your class file under classes/com/HelloWorld folder 将您的课程文件放在classes / com / HelloWorld文件夹下
  • restart the tomcat 重新启动tomcat

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

项目名称[] / 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文件:
将创建新的.class文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在固定继承人的“ getters”和“ setters”时遇到问题 - I'm having problems fixing my “getters” and “setters” in inheritance 我的 android 应用程序中的地理位置存在问题 - I'm having problems with the geolocation in my android app 我在MySQL数据库上更新BLOB时遇到问题 - I'm having Problems Updating BLOB on my mySQL database 这是我第一次使用 Swing,我在使用 JPanel 时遇到了问题 - This is my first time using swing and I'm having issues with the JPanel 我在矩形碰撞检测方面遇到问题,它似乎只能得到第一个对象 - I'm having problems with rectangle collision detection it only seems to get the first object 我在使用 Regex 模式时遇到问题 - I'm having problems with Regex patterns 我在访问if语句中初始化的总价格时遇到问题 - I'm having problems accessing the total price I initialized inside my if else statements 我在我的 android studio 上运行我的第一个 java 程序时遇到问题 - I am having problems running my first java program on my android studio 当我尝试在 Spring 引导应用程序中发送嵌套实体的 PUT 请求时,我的 REST 端点中出现 NullPointerException 问题 - I'm having problems with NullPointerException in my REST endpoints when I try to send a PUT Request for a nested entity in Spring Boot Application 我的几何程序有问题 - I'm having trouble with my geometry program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM