从客户端到Java Servlet的GET / POST请求中找不到404错误 [英] 404 Not Found Error on GET/POST Request from Client to Java Servlet

查看:138
本文介绍了从客户端到Java Servlet的GET / POST请求中找不到404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am receiving a 404 error (in the web browser console) upon sending a GET request from an html page via jquery to my java servlet .class file.

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

I honestly don't know what I should be doing after creating the core files in my IntelliJ maven-webapp project:

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

我正在构建我的通过以下方式投影到名为target的文件夹:

I am building my project to a folder called "target" via:


构建>构建工件>>所有工件>构建

Build > Build Artifacts >> All Artifacts > Build

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

And then, I take all the files in target and upload them to my department's server, but when I navigate to index.html and click the button, it returns a 404 error on the GET request instead of printing out the text specified in the 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文件:



"index.html" File:

<!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文件:



"do.js" File:

/**
 * 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文件:



"web.xml" File:

<!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文件:



"pom.xml" File:

<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的好资源。谢谢!


Can someone guide me as to what I should change to make this work, I feel I am quite close to a solution here? Also let me know if there are better ways about going about this problem, I have briefly heard of web servlet 3.0 but could not find any good resources on it for IntellliJ and Maven. Thanks!

推荐答案

正如CrazyCoder所指出的那样,我的问题是在do。中的jquery.get(...。 jsfile。应该是:

As CrazyCoder pointed out, my problem was at "jquery.get(..." in the "do.js" file. It should be:

$。get(WEB-INF\\classes \\ MyTestServer.class ...

而不是:

$ .get(WEB-INF \\classes \\ServerTest.class...

这只是一个简单的拼写错误!

It's just a simple typo!

这篇关于从客户端到Java Servlet的GET / POST请求中找不到404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆