【问题标题】:how to pass string message from servlet and catch it in HTML file using json?如何从 servlet 传递字符串消息并使用 json 在 HTML 文件中捕获它?
【发布时间】:2015-02-17 11:06:05
【问题描述】:

我有一个原始问题,我试图在 doGet 函数内的 servlet 中发送字符串消息,然后使用 JSON 在 HTML 函数中捕获此消息?有什么帮助吗?

【问题讨论】:

  • 字符串messege="on"; Gson gson = 新 Gson(); gson.toJson(messege); response.setContentType("应用程序/json"); PrintWriter out1 = response.getWriter(); out1.print(gson.toJson(messege)); out1.flush();

标签: html json servlets


【解决方案1】:

首先,如果你在doGet中插入你的代码,你需要在ajax中做一个GET请求。

您需要在 web inf 文件中为您的 servlet 提供一个 URL。

然后你需要一个 javascript 文件(链接在你的 html 中)。这个js文件会有这样的smt。

$(document).ready(function(){
    $.get( "urlInWebInf", function( data ) {
       console.log(data);
    });
)}

它应该在控制台中显示您的 json。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-02
    • 2022-01-27
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多