I am trying to pass a JsonObject to jsp. However, I think I cannot get the JsonObject using getAttribute method. How should I do to make this available.
我试图将JsonObject传递给jsp。但是,我认为我不能使用getAttribute方法获得JsonObject。我该怎么做才能使它可用。
There's a Servlet code, below.
下面是一个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");
}
}
There's a jsp code below.
下面有一个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>
Thanks in advance.
提前谢谢。
5 个解决方案
#1
1
Here just a sample by use google Gson library. you can download that lib here or here
这里只是使用谷歌Gson库的一个示例。你可以在这里或这里下载这个库
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);
}
Anyway, you can use Ajax to loop data or use JSTL!
无论如何,您可以使用Ajax循环数据或使用JSTL!
#2
0
@The Neo Noir Develper
@The Neo黑色患病
Even I changed redirection to forward It doesn't work and make same error.
即使我改变了重定向前进它也不能工作,并犯同样的错误。
Here's servlet code below.
这是下面的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");
}
There's jsp code below.
有下面的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>
#3
0
Could you please try to use the below code in your servlet instead of sendRedirect
您可以尝试在您的servlet中使用以下代码而不是sendRedirect吗
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
and also check the jsonObject in your jsp for null either you are getting null or not from session attribute,
还可以检查jsp中的jsonObject是否为null也可以检查session属性是否为null,
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
after doing above changes if you will again get the error then please post the error stack.
在做了以上修改后,如果你将再次得到错误,然后请张贴错误堆栈。
#4
0
Can you give a try to change the contect type of your response
你能不能试着改变一下你的反应类型
response.setContentType("application/json");
#5
0
There are multiple things wrong in your code. Apart from using scriptlets.
在您的代码中有许多错误。除了使用scriptlet。
Anyhoo, first, are you sure your session is being carried over? I say this because you are using a redirect instead of a simple RequestDispatcher Forward. A redirect creates a new request.
无论如何,首先,你确定你的会话正在进行中吗?之所以这样说,是因为您使用的是重定向,而不是简单的RequestDispatcher Forward。重定向创建一个新的请求。
I think you should set this object in request and use a RequestDispatcher. Similarly, on the JSP side, you should use the request object to get the attribute back.
我认为您应该将这个对象设置为request并使用一个RequestDispatcher。类似地,在JSP端,您应该使用request对象来获取属性。
Without much info of how the session is created, it would be hard to pinpoint whether the session is being maintained at all or not.
如果没有关于如何创建会话的大量信息,就很难确定会话是否被维护。
#1
1
Here just a sample by use google Gson library. you can download that lib here or here
这里只是使用谷歌Gson库的一个示例。你可以在这里或这里下载这个库
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);
}
Anyway, you can use Ajax to loop data or use JSTL!
无论如何,您可以使用Ajax循环数据或使用JSTL!
#2
0
@The Neo Noir Develper
@The Neo黑色患病
Even I changed redirection to forward It doesn't work and make same error.
即使我改变了重定向前进它也不能工作,并犯同样的错误。
Here's servlet code below.
这是下面的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");
}
There's jsp code below.
有下面的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>
#3
0
Could you please try to use the below code in your servlet instead of sendRedirect
您可以尝试在您的servlet中使用以下代码而不是sendRedirect吗
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
and also check the jsonObject in your jsp for null either you are getting null or not from session attribute,
还可以检查jsp中的jsonObject是否为null也可以检查session属性是否为null,
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
after doing above changes if you will again get the error then please post the error stack.
在做了以上修改后,如果你将再次得到错误,然后请张贴错误堆栈。
#4
0
Can you give a try to change the contect type of your response
你能不能试着改变一下你的反应类型
response.setContentType("application/json");
#5
0
There are multiple things wrong in your code. Apart from using scriptlets.
在您的代码中有许多错误。除了使用scriptlet。
Anyhoo, first, are you sure your session is being carried over? I say this because you are using a redirect instead of a simple RequestDispatcher Forward. A redirect creates a new request.
无论如何,首先,你确定你的会话正在进行中吗?之所以这样说,是因为您使用的是重定向,而不是简单的RequestDispatcher Forward。重定向创建一个新的请求。
I think you should set this object in request and use a RequestDispatcher. Similarly, on the JSP side, you should use the request object to get the attribute back.
我认为您应该将这个对象设置为request并使用一个RequestDispatcher。类似地,在JSP端,您应该使用request对象来获取属性。
Without much info of how the session is created, it would be hard to pinpoint whether the session is being maintained at all or not.
如果没有关于如何创建会话的大量信息,就很难确定会话是否被维护。