【发布时间】:2017-02-19 19:44:37
【问题描述】:
谁能给我一个 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 文件,而是在页面上显示数据。
有人可以告诉我如何实现该场景吗?
【问题讨论】: