有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java演示简单Ajax ans Servelt实现失败

我试图演示简单的Ajax/Servlet请求。为此,我在eclipse中创建了一个新的动态web项目,并添加了一个简单的Servlet来接收请求并将其返回到UI。我已经在web中包含了我的Servlet详细信息。xml。我使用Tomcat作为我的服务器。还没有构建脚本(此时我觉得不需要)

Servlet代码:

response.setContentType("text/html");
    PrintWriter out =response.getWriter();
    String txt = request.getQueryString();
    out.println(txt);

Js代码:

$(function(){
        $.get('/jirarequest','OK',function(op){
            $('#maindiv').appendTo(op);
        });

    });

Html代码:

<?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"
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>Jira-Synchronization</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>JiraSyncServlet</servlet-name>
    <servlet-class>src.JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>JiraSyncServlet</servlet-name>
    <url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>

我的项目结构:enter image description here

我的HTML:

enter image description here

我在Fire bug中得到一个错误

"NetworkError: 404 Not Found - http://localhost:8080/jirarequest?OK"

我在浏览器中使用的URL是http://localhost:8080/Jira-Synchronization/index.html。我仔细检查了所有的打字错误。我通过设置断点进行调试,但调试器从未命中我的服务器。我不知道怎么了?URL错误或接线错误,或其设置方式缺失


共 (1) 个答案

  1. # 1 楼答案

    一些信息:

    • 404错误意味着Tomcat没有任何文件、servlet或任何暴露在该URL路径上的内容

    • 500错误意味着Tomcat在该URL路径上公开了一个servlet(或jsp),并且在尝试运行java代码时生成了一个意外的异常

    我们知道需要更改的内容集合:

      $.get('/jirarequest/whatever','OK',function(op){
    

    <servlet>
        <servlet-name>JiraSyncServlet</servlet-name>
        <servlet-class>JiraSyncServlet</servlet-class>
    </servlet>
    

    此外,您还可以通过键入此URL在浏览器中进行测试,该URL应返回OK:

    http://localhost:8080/Jira-Synchronization/jirarequest/whatever?OK