Tuesday, January 15, 2013

get the arraylist value from servlet to jsp

Servlet:
PrintWriter out = response.getWriter();
        out.println("test");
        BufferedReader buffer = new BufferedReader(new FileReader(new File("C:/Users/boomiraj/Desktop/Boomiraj1.csv")));
        String readline = buffer.readLine();
        ArrayList<String> array = new ArrayList<String>();
       
        while(readline != null){
            String[] read = readline.split(" ");
            //out.println(read);
        array.add(read[0]);
        array.add(read[1]);
        array.add(read[2]);
        //array.add("sekar");
        Iterator<String> i = array.iterator();
        while(i.hasNext()){
            i.next();
        }
        readline = buffer.readLine();
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("test", array);
        RequestDispatcher rd;
        rd = request.getRequestDispatcher("checkout.jsp");
        rd.forward(request,response);
        //response.sendRedirect("");



JSP file checkout.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*"%>
<!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>Insert title here</title>
</head>
<body>
<%
ArrayList<String> array =(ArrayList)session.getAttribute("test");
if(!array.isEmpty()){
    for(int i = 0; i<array.size(); i++){
        %>
        <td><%= array.get(i)%></td>
       
       
<%}%><br>
    <%}%>       
</body>
</html>

1 comment: