Reputation: 187
I have a jsp page where I am trying to get a JSON object from my servlet.
jsp code:
<%@page import="org.codehaus.jettison.json.JSONObject"%>
<!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)request.getAttribute("jsonObject");
%>
</head>
<body>
<h6>JSON View</h6>
<%=jsonObject%>
</body>
</html>
My java code sends the json object to the above jsp page:
JSONObject jsonObj = new JSONObject(jsonString.toString());
request.setAttribute("jsonObject", jsonObj);
RequestDispatcher dispatcher = request.getRequestDispatcher("check.html");
dispatcher.forward(request, response);
My jsp page is displaying all scriptlets instead of the json data. Please advice. Thanks.
I see this error in jsp page:
java.lang.ClassCastException: org.codehaus.jettison.json.JSONObject cannot be cast to org.json.simple.JSONObject
Upvotes: 0
Views: 475
Reputation: 4029
Change import statement in jsp
From
<%@page import="org.codehaus.jettison.json.JSONObject"%>
to
<%@page import="org.json.simple.JSONObject"%>
Upvotes: 1