简体   繁体   English

如何在response.sendRedirect中传递集合对象

[英]how to pass a collection object in response.sendRedirect

in a jsp page say x.jsp ,i have 在一个jsp页面说x.jsp,我有

if (some task =true)
{
//show xml at run time
List <User_Registration> list = dao.selectemailmobile(login); //here m getting data from  Db and m getting it perfectly alright
response.sendredirect("page.jsp?list="+list)
//i wanted to pass this list parameter as List<Bean> list=List<Bean>();
//this page will show xml at run time

}
else 
{
//some status code 
//this working fine 
}

what i tried 我尝试过的

1.pass this(List) parameter as list i have shown,but when i'm retrieving it, is says 1.传递this(List)参数作为我显示的列表,但是当我检索它时,说

 cannot cast from string to List<Bean>

for that i also tried to type cast it,but nothing happens. 为此,我也试图键入强制转换,但什么也没有发生。

2.setting that List in session it shows 2.在显示的会话中设置该列表

unchecked cast from object to List<Bean>

3.using request Dispatcher 3.使用请求分派器

 i get same cannot cast from string to List<Bean>

my code for that xml page 我的XML页面的代码

<%@ page language="java" contentType="text/xml; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="com.xml.*" %>
<%@page import="com.beans.*" %>
<%@page import="java.util.*" %>


<%@page import="com.xml.ForgotPasswordXml"%>
<%!java.util.List<User_Registration> list= null; %>
<%try{
HttpSession session2=request.getSession();
System.out.println("hii");

list=(List<User_Registration>)request.getAttribute("list");
//list=(java.util.List<User_Registration>)request.getParameter("list");
System.out.println(list.toString());
}catch(Exception e)
{
e.printStackTrace();
}
%>
<%=ForgotPasswordXml.xml(list)%>

any help would be appreciated.thnks 任何帮助将不胜感激。

You can set the list in session as 您可以将会话中的列表设置为

request.getSession().setAttribute("list",list);

And in the next page retrieve the list as 并在下一页中检索列表为

@SuppressWarnings("unchecked")
List<Bean> list = (List<Bean>) request.getSession().getAttribute("list");

or 要么

List<Bean> list = new ArrayList<Bean>();  
list.addAll(session.getAttribute("list")); 

Note the type cast, it will avoid Type mismatch exception. 注意类型强制转换,它将避免类型不匹配异常。

And if the list isn't needed further, remove it from the session as (optional) 如果不再需要该列表,则将其从会话中删除为(可选)

request.getSession().removeAttribute("list");

Send the whole List as one JSON string and rebuild the List using the JSON . 将整个列表作为一个JSON字符串发送,并使用JSON重建列表。 You need to use JSONObject . 您需要使用JSONObject。 The other thing you can do is , set List to Session and get back the List from the session and invalidate the Session if requires . 您可以做的另一件事是,将List设置为Session,然后从会话中获取List,并在需要时使Session无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM