gpt4 book ai didi

java - 从客户端到 Java Servlet 的 GET/POST 请求出现 404 Not Found 错误

转载 作者:行者123 更新时间:2023-11-30 02:28:05 27 4
gpt4 key购买 nike

通过 jquery 从 html 页面向我的 java servlet .class 文件发送 GET 请求时,我收到 404 错误(在 Web 浏览器控制台中)。

老实说,我不知道在我的 IntelliJ maven-webapp 项目中创建核心文件后应该做什么:

pom.xml, MyTestServlet.class, web.xml, index.html, do.js

我正在将项目构建到名为“target”的文件夹中:

Build > Build Artifacts >> All Artifacts > Build

然后,我获取 target 中的所有文件并将它们上传到我部门的服务器,但是当我导航到 index.html 并单击按钮时,它会在 GET 请求上返回 404 错误,而不是打印出指定的文本在 servlet 中。

<小时/>

这是项目的文件结构:

enter image description here

<小时/>

这是结果的文件结构:

enter image description here

<小时/>

“MyTestServlet.java”文件(Java Servlet):

import java.io.IOException;

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

/**
* Created by isardar on 7/11/2017.
*
* GOAL FOR THIS TEST:
*
* make a servlet that sends data to client and client should receive/process the data
*
*/
//@WebServlet("/MyTestServlet") <-- idk what i'm doing
public class MyTestServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text boiii";
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(text); // Write response body.
}

}

“index.html”文件:

<!DOCTYPE html>
<html lang="en">
<head>
<title>SO question 4112686 - Copied & Tweaked Version</title>
<link rel="stylesheet" href="style.css"/>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="do.js"></script>
</head>
<body>
<button id="somebutton">press here</button>
<div id="somediv"></div>
</body>
</html>

“do.js”文件:

/**
* Created by isardar on 7/11/2017.
*/
$(document).on("click", "#somebutton", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
$.get("WEB-INF\\classes\\ServerTest.class", function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$("#somediv").text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});

“web.xml”文件:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>MyTestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

“pom.xml”文件:

<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>ServletTest4</groupId>
<artifactId>ServletTest4</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ServletTest4 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>

<!-- a default maven-web-app dependency for some reason... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>

</dependencies>
<build>
<finalName>ServletTest4</finalName>
</build>
</project>
<小时/>

有人可以指导我应该改变什么才能使这项工作正常进行吗?我觉得我已经非常接近解决方案了?另外请告诉我是否有更好的方法来解决这个问题,我曾短暂听说过 Web Servlet 3.0,但找不到任何关于 IntellliJ 和 Maven 的好资源。谢谢!

最佳答案

确保您的应用程序正在应用程序服务器(Tomcat、JBoss 等)下运行后。您的请求应重定向到正确的路径,该路径应类似于:localhost:8080/ProjectName/WebServletName。

您正在使用get请求直接访问servlet文件,但它工作在HTTP协议(protocol)下。所以应该以这种方式访问​​

关于java - 从客户端到 Java Servlet 的 GET/POST 请求出现 404 Not Found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45064661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com