我正在尝试将一个JsonObject传递给jsp。但是,我认为我不能使用getAttribute方法获得JsonObject。我应该怎么做才能使它可用。
下面是一段Servlet代码。
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@WebServlet("/ShapeRendererFileManager")
public class ShapeRendererFileManager extends HttpServlet {
private static final long serialVersionUID = 1L;
HttpSession session;
//Send Json to jsp
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
response.sendRedirect("index.jsp");
}
}下面有一段jsp代码。
<%@page import="com.google.gson.JsonObject"%>
<%@page import="com.google.gson.JsonElement"%>
<%@page import="com.google.gson.JsonArray"%>
<!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=EUC-KR">
<title>start page</title>
</head>
<%
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
String tmp = "";
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
tmp = json.get("name").toString(); //error at this line
%>
<body>
<script>
$(function(){
document.getElementByName("formtag").action="ShapeRendererFileManager";
document.getElementById("formtag").submit();
})
</script>
<h1><%= tmp %></h1>
<form name="formtag" action="" method="post">
</form>
</body>
</html>提前谢谢。
发布于 2015-11-26 13:45:32
这里只是一个使用google Gson库的示例。您可以下载该库here或here
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap<Object, Object> obj=new HashMap<>();
obj.put("name", "Janny");
obj.put("title", "Coder");
String jsonObject=new Gson().toJson(obj);
System.out.println(jsonObject);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
session.setAttribute("jsonObject",jsonObject);
request.getRequestDispatcher("index.jsp").forward(request, response);
}无论如何,您可以使用Ajax循环数据或使用JSTL!
发布于 2015-11-26 13:29:28
@ Neo Noir Develper
即使我更改了重定向以转发它也不起作用,并且犯了同样的错误。
下面是servlet代码。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
//response.sendRedirect("index.jsp");
}下面是jsp代码。
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<%@page import="com.google.gson.JsonObject"%>
<%@page import="com.google.gson.JsonElement"%>
<%@page import="com.google.gson.JsonArray"%>
<!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=EUC-KR">
<title>start page</title>
</head>
<%
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
String tmp = json.get("name").toString(); //error at this line
%>
<body>
<!-- <div id="main_page">
</div>
<script data-main="main" src="libs/require-2.1.11.js"></script> -->
<script>
$(function(){
document.getElementByName("formtag").method="POST";
document.getElementByName("formtag").action="ShapeRendererFileManager";
document.getElementById("formtag").submit();
});
</script>
<h1><%= tmp %></h1>
<form name="formtag" action="" method="post">
</form>
</body>
</html>发布于 2015-11-26 13:50:05
您可以尝试在servlet中使用以下代码而不是sendRedirect吗
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);还要检查jsp中的jsonObject是否为null,或者是从会话属性获取的是null,
JsonObject json = (JsonObject)session.getAttribute("jsonObject");完成上述更改后,如果您将再次收到错误,请发布错误堆栈。
https://stackoverflow.com/questions/33930642
复制相似问题