0

在 servlet 中,我创建了一个 JSONObject 并将该对象传递给 JSP。

JSONObject jo=new JSONObject();
jo.put("site","java4s.com");
jo.put("content","Java");
jo.put("TotalLinks",927);
HttpSession session=request.getSession(true);
session.setAttribute("jsonObject", jo);
RequestDispatcher rd = request.getRequestDispatcher("viewpage.jsp");
rd.forward(request, response);

这是我的 JSP 页面

<%@page import="com.google.gson.JsonObject"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Json</title>
<%
JSONObject jsonObject=(JSONObject)session.getAttribute("jsonObject");
%>

</head>
<body>
<h6>JSON View</h6>
<br>
<%=jsonObject%>
</body>
</html>

但是 JSP 页面显示JSONObject无法解析为类型的错误。但是我已经添加了 jar 文件,并且在 Servlet 中它没有显示错误。我该怎么办?谢谢

4

1 回答 1

4

将页面导入:更改<%@page import="com.google.gson.JsonObject"%><%@page import="com.google.gson.JSONObject"%>

注意JSONin JSONObject

于 2013-03-18T13:40:05.580 回答