Javascript 不使用servlet将JSON对象传递给Java类

Javascript 不使用servlet将JSON对象传递给Java类,javascript,java,json,ajax,web,Javascript,Java,Json,Ajax,Web,我在HTML中有一个表单,我使用JavaScript获取该表单的值。我将其转换为对象,如下所示(仅在JavaScript中): 现在,我希望使用Ajax(或任何其他相关内容)将此对象发送到我拥有的Java类(不是SERVLET)。我试图通过以下方式做到这一点: $.ajax({ url: 'Resource', type: 'POST', dataType

我在
HTML
中有一个表单,我使用
JavaScript
获取该表单的值。我将其转换为对象,如下所示(仅在
JavaScript
中):

现在,我希望使用
Ajax
(或任何其他相关内容)将此对象发送到我拥有的
Java
(不是SERVLET)。我试图通过以下方式做到这一点:

    $.ajax({
    url: 'Resource',
    type: 'POST',                                                      
    dataType :'json',
    data: obj1,
    success: function(result) {
        alert('SUCCESS');
    },
    error: function(){
        alert('Error');
}});
但是上面的代码似乎不起作用。F12
调试器(浏览器的调试工具)显示:
错误404:未找到资源。

有什么建议说明为什么这行不通吗?我希望从我的
JavaScript
发送对象,并在我的Java代码中接收它,以便进一步处理。另外,请注意,我使用的是
IE

编辑: 以下是我的
资源.java

    public class Resource extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public Resource(String obj1) {
        // TODO Auto-generated constructor stub
        System.out.println(obj1);
        System.out.println("inside resource!");
    }
}
web.XML

    <?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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" metadata-complete="true" version="3.0">
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
</web-app>

欢迎来到Tomcat
欢迎来到Tomcat

您的404指示服务器无法匹配请求URL处的资源。您说您不希望使用Servlet,但正在尝试发布到Servlet


您的URL应该类似于/logHandler/。在您使用的任何JavaWeb框架中,您都会将servlet(或其他处理程序)映射到该URL。基本上,您无法直接从客户端寻址类。

您的java代码运行的是什么?url:'ResourceServlet.java',不正确,请共享该类。请检查我的编辑@saurabhjunjunhunlai,了解404显示它找不到该类。但由于该类位于默认包中,我无法找出它不起作用的原因。使用
document.location.href=Resource是否有同样的方法?我相信它会转到resource,但我不知道如何随它一起传递JSON对象。有什么建议吗?你不能用这种方式处理一个类-它首先违反了几个安全问题。您使用的是什么框架?您有web.xml吗?我认为您需要遵循一些基本的servlet教程,以便了解您的问题。我不能用简短的回答来解决你的问题。
    <?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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" metadata-complete="true" version="3.0">
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
</web-app>