- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过 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 中。
<小时/>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.
}
}
<!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>
/**
* 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.
});
});
<!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>
<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/
我们在日常使用电脑的时候,有些情况下可能会遇到出现提示found.000文件丢失损坏的情况。那么如果想要对found.000文件进行恢复,小编觉得可以在我们的文件夹选项中进行相关设置,找到被隐藏的文
下面的代码给出了“未找到”的输出。但我希望它能给出“发现”。我的错误在哪里? #include void compare(char *x, char *face); int i; int main(
为什么我的 git 不推送?我创建了存储库,但不断收到此消息: C:\Users\petey_000\rails_projects\first_app>git push -u github maste
我正在使用 Spring Boot 构建 Web API。端点之一采用 JSON 发布请求。我想在某些字段上设置验证消息。所以我在/src/main/resources 下添加了一个 Validati
我按照此页面上的说明进行操作: https://developers.facebook.com/docs/plugins/share-button/#settings 我得到一个工作共享对话框,但是当
如何在事件监听器中触发重定向到特定路由器? 有很多示例,但我找不到“GetResponseForExceptionEvent”的示例。例如,当我将 @roter 作为参数传递时,$this->rout
我使用下面的代码来发布 ssl 网络服务。当我浏览 https://localhost/ 它给我这个错误: 404 Not Found No context found for request 代码:
我正在尝试构建一个 docker 镜像但是正在获取 未找到 secret pip:未找到 对此有什么想法吗? docker 文件: FROM SHELL ["/bin/bash", "-c"] RU
我正在使用 indy TIDHTTP 编写一种方法来了解我在互联网上的服务器是否已关闭或同一服务器上的页面地址不可用。 我复制了 stackoverflow 上另一个线程中给出的建议: try I
我尝试将结果附加到列表中(如果找到),并将字符串“N/A”附加到列表中(如果未找到匹配项)。 show_version_lists 是来自各种网络设备的日志列表。 import re for resu
我在这里尝试加载关于我们的页面。但是找不到请求的页面。请提供此问题的解决方案。 查看: ">HOME /CI_timeline/
我不会问问题,而是回答一个问题,因为到目前为止我在网上找不到这个建议,并且自己刚刚弄清楚了这一点。分享是件好事,对吧? 所以在命令行上,我这样做了: sudo npm install -g cordo
我有一个脚本来处理一些参数并使用这些参数部署 Arm 模板,然后使用我的存储帐户中的 bacpac 文件创建数据库。当我的脚本尝试调用 cmdlet“New-AzSqlDatabaseImport”时
我使用“chrome://inspect”几天了。一切都很好。今天,由于某种原因,当我单击“检查”时,结果是“404 Not Found 找不到资源。” 这可能是什么原因? 我只能找到这个主题:htt
我的 Ubuntu 14.04 有一个奇怪的问题。我正在使用 Django-Rest-Framework 开发一个使用 Ionic 和 API 的移动应用程序。开发完成后,我下载了 jdk 和 And
我正在使用calabash-android-java测试使用 Xamarin 构建的混合应用程序。正如您在下面看到的,我能够查询 DOM 元素,甚至将一些信息记录到控制台,但是当我执行 touch 命
搜索功能非常有用,但我不确定如何为其添加某些“附加功能”。当有结果显示时,我还想说: "Your search returned x results." 后面是结果。 当没有结果显示时,我想说: "Y
我试图在我的应用程序中实现导航功能。当我尝试构建应用程序时,输出显示: Android resource linking failed /home/mateusz/AndroidStudioProje
我需要查询公共(public)项目提供的数据集。我创建了自己的项目并将他们的数据集添加到我的项目中。有一个表名为:domain_public .当我对该表进行查询时,我收到此错误: Query Fa
我对 Node.js 相当陌生,我使用的是下面页面中的教程。 http://www.9lessons.info/2017/02/create-restful-api-nodejs-mysql.html
我是一名优秀的程序员,十分优秀!