簡體   English   中英

從JavaScript到servlet的Ajax調用

[英]Ajax call from JavaScript to servlet

我正在嘗試從html文件向servlet類進行AJAX調用以傳遞變量,但未獲得任何輸出。 請檢查下面的代碼,並提出應做的更改。

這是我的項目結構

FrontPage.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
    function func() {

            var build = document.getElementById('name').textContent;

            $.ajax({

                type : 'GET',
                url : 'http://localhost:8081/Sample/TestServlet',
                data : build,

                success : function(data) {
                    alert("Success");

                },
                error : function() {
                    alert("Error");
                }
            });
        }
    </script>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>

    <a id="name" href="#" onClick="func();">Build1</a>

</body>
</html> 

這是TestServlet.java 這試圖打印使用Ajax調用從javascript函數傳遞的變量:

package com.infy;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        out.println(request.getParameter("build"));

    }

}

Web.xml文件

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Sample</display-name>
      <welcome-file-list>
        <welcome-file>FrontPage.html</welcome-file>   
      </welcome-file-list>
      <servlet>
        <description>Sample Servlet</description>
        <display-name>TestServlet</display-name>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.infy.TestServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
      </servlet-mapping>
    </web-app>

我想將build變量發送到servlet類,並從servlet類中打印出來,但是沒有任何輸出。請幫助我解決此問題。

您正在以GET方法發送請求,並且沒有在url中傳遞變量。請嘗試先在servlet中打印構建值,然后檢查天氣是否即將到達servlet。

您應該以以下方式發送GET請求:

url : "/TestServlet?build="+build+",
            type : "GET"

,還要注意網址。 在servlet站點中創建一個json對象。

JSONObject jsonObj = new JSONObject();

jsonObj.put("build", build);

試試看,讓我知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM