使用 Tomcat 服务器运行 Servlet 类时出现 HTTP Status 400 错误

问题描述 投票:0回答:2

我对编写 servlet 完全陌生。我创建了一个简单的 HelloWorld 程序,但在运行该应用程序时出现以下错误:

HTTP 状态 404 - /HelloWorld/HelloWorld

输入状态报告

消息/HelloWorld/HelloWorld

描述:请求的资源不可用。

下面是我的代码:

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

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


@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;           
    
    public HelloWorld() {
        super();  
    }    
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        out.println("Hello World");
    }
}

还有我的 web.xml

<display-name>HelloWorld</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

我使用的是Tomcat版本:7.0,我的IDE是Eclipse Luna。为什么会抛出这个错误?

java xml servlets
2个回答
0
投票

your_war_file_name/HelloWorld... 您可以在 Tomcat 目录的 webapps 文件夹下找到 war 文件名。在这里我只是重新创建了相同的内容。我的战争文件名为 website.war。这个对我有用。网址是 http://localhost:8090/website/developer 。我收到了回复“Hello World”....:)

package com.test.developer;

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 Test extends HttpServlet{

    private static final long serialVersionUID = 1L;


    public Test() {
        super();

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Inside servelt");
        PrintWriter out=response.getWriter();
        out.println("Hello World");
    }
}

我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>website</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>developer</servlet-name>
  <servlet-class>com.test.developer.Test</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>developer</servlet-name>
  <url-pattern>/developer</url-pattern>
  </servlet-mapping>
</web-app>

0
投票

您应该从浏览器访问您的 servlet 文件,而不是直接从 IDE 使用此链接格式 http://domain-name/virtual-directory/servlet/servlet-name tomcat 默认在端口 8080 上运行,但如果您因该端口被占用而不得不更改端口,那么您就是将其更改为的端口。

示例:

package servlets;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;


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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Welcome to servlets");
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

我的 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0">
  <servlet>
    <description></description>
    <display-name>HelloWorld</display-name>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>servlets.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
  <display-name>servlets</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
当我使用链接从浏览器访问 servlet 时 http://localhost:8082/servlets/HelloWorld 我有一个美丽的欢迎来到 servlet:)

但是当我直接从 Eclipse IDE 运行该文件时,出现 http status 400 错误:(

快乐编码!!

© www.soinside.com 2019 - 2024. All rights reserved.