簡體   English   中英

如何加載JSP頁面並同時在json文件上寫入數據?

[英]How to load a JSP page and write data on a json file simultaneously?

有人可以給我一個JSP頁面的示例,該示例包含一個包含一些數據的數組變量,並且每次加載JSP頁面時,數組中的那些數據都將寫入特定目錄下的json文件中。 然后,我將使用另一個html頁面處理json文件中的數據。

稍后,我將與數據庫連接到JSP頁面。 但是,首先我必須在沒有任何數據庫的情況下進行處理。 因此,想法是何時更新我的​​數據庫; JSP頁面將自動更新json文件(根據我的預期情況),並且隨着JSP頁面的每次加載(或單擊某些按鈕),我每次都可以使用不同的數據集進行處理。

我有點困惑..這種情況是否可能?

而且,我還嘗試了一些代碼,例如:

<%-- Set the content type header with the JSP directive --%>
<%@ page contentType="application/json" %>

<%-- Set the content disposition header --%>
<%
// Returns all employees (active and terminated) as json.
response.setContentType("application/json");
response.setHeader("Content-Disposition", "inline");
%>

<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="javax.servlet.http.*"%>


[
{"label":"item 1", "value":"item 1", "id": 1},
{"label":"item 2", "value":"item 2", "id": 2},
{"label":"item 3", "value":"item 1", "id": 3}
]

但是,我無法通過任何這些代碼在json文件中寫入數據,而是在頁面上顯示數據。

有人可以告訴我如何實施該方案嗎?

我已經解決了問題。

假設您要在jsp頁面中寫入一些列值。 只需使用... json.dumps()和json.load函數。

這是個例子

 import json data = { 'name' : 'ACME', 'shares' : 100, 'price' : 542.23 } json_str = json.dumps(data) data = json.loads(json_str) # Writing JSON data with open('data.json', 'w') as f: json.dump(data, f) # Reading data back with open('data.json', 'r') as f: data = json.load(f) 

暫無
暫無

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

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