此URL不支持HTTP方法GET - Tomcat

时间:2015-06-19 10:56:44

标签: java ajax eclipse tomcat

我知道此前已经多次询问过这个问题,但在阅读了其他几个问题的答案和解决方案之后,我无法解决我的问题。

我目前正在使用一台我没有管理权限的计算机,我最好的猜测是防火墙阻止了本地主机上的服务器。

这是我的代码:

GetTimeServlet.java

<?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" version="3.0">
  <display-name>exjobb</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>GetTime</servlet-name>
        <servlet-class>servlets.GetTimeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GetTime</servlet-name>
        <url-pattern>/get-current-time</url-pattern>
    </servlet-mapping>
</web-app>

Web.xml中

onClick="ajaxAsyncPostRequest('http://localhost:8080/ajaxdemo/get-current-time', $('#temp').val())

的index.html

我有一个按钮,它运行以下功能 function ajaxAsyncPostRequest(reqURL, temp) { temp = encodeURIComponent(temp); var params = "site=" + temp + "&userrating=" + $("#slider").val() + "&usercomment=" + $("#comment-textarea").val() + "&chkbox1=" + $("#sq_checkbox1").val() + "&chkbox2=" + $("#sq_checkbox2").val(); //Creating a new XMLHttpRequest object var xmlhttp; if (window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); //for IE7+, Firefox, Chrome, Opera, Safari } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //for IE6, IE5 } //Create a asynchronous GET request xmlhttp.open("POST", reqURL, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //When readyState is 4 then get the server output xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { gauge.refresh(xmlhttp.responseText); //alert(xmlhttp.responseText); } else { //alert(xmlhttp.status); } } }; xmlhttp.send(params); }

{{1}}

表单数据显然是在Post中发送的,服务器只处理Post请求。我在Eclipse中没有收到任何错误或警告。这是怎么回事?

编辑:我正在尝试发帖并且 GET。

1 个答案:

答案 0 :(得分:1)

对于HTTP GET方法,您应该在servlet中定义doGet方法(GetTimeServlet.java);