从客户端到Java Servlet的GET / POST请求中找不到404错误

时间:2017-07-12 17:59:52

标签: java jquery maven servlets intellij-idea

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

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

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

我正在将项目构建到名为" target"的文件夹中。通过:

  

构建>构建工件>>所有文物>构建

然后,我将目标中的所有文件都上传到我部门的服务器,但是当我导航到index.html并单击该按钮时,它会在GET请求中返回404错误而不是打印out出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.
    }

}

&#34;的index.html&#34;文件:

<!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>

&#34; do.js&#34;文件:

/**
 * 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.
    });
});

&#34;的web.xml&#34;文件:

<!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>

&#34; pom.xml的&#34;文件:

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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

与服务器启动时未识别的servlet有关。它看起来是一个与Servlet相关的问题可能不在正确的目录或包中。或者您还必须在“web.xml”文件中提供servlet的包名称

答案 2 :(得分:0)

正如CrazyCoder所指出的那样,我的问题出现在“do.js”文件中的“jquery.get(...”。应该是:

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

而不是:

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

这只是一个简单的错字!