Ashes Ashes - 2 months ago 29
Java Question

how to pass a collection object in response.sendRedirect

in a jsp page say x.jsp ,i have

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

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

unchecked cast from object to List<Bean>


3.using request Dispatcher

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


my code for that xml page

<%@ 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

Answer

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");