-1

I'm trying to set a Array list of String to an attribute of session in servlet, and try to access this Array list of Attributes in jsp. But just one value(last value) access in jsp. i want to access All list of attribute.

i searched too much here and there but i don't found about my question.

form jsp:

<form action="/CompleteServlet" method="get">
<%String completeTasks=((ArrayList<String>)session.getAttribute("todoList")).get(i);%>
 <input type="hidden" name="completeTasks" value="<%=completeTasks%>" />
 <input type="submit" value="Completed">
</form>

from CompleteServlet :

 String v=req.getParameter("completeTasks");
        HttpSession session=req.getSession();

        ArrayList<String> arrOfCompleteTask = new ArrayList<>();
        arrOfCompleteTask.add(v);
        session.setAttribute("completeTasks", arrOfCompleteTask);

        req.getRequestDispatcher("/complete.jsp").forward(req,resp);

form complete.jsp

<%
int size=((List<String>)session.getAttribute("tryCom")).size();
    for(int i=0;i<size;i++)
        {%>
<%=((List<String>)session.getAttribute("tryCom")).get(i)%>``
       <%}%>
RAna UzAir
  • 47
  • 4
  • <% int size=((List)session.getAttribute("completeTasks")).size(); for(int i=0;i <%=((List)session.getAttribute("completeTasks")).get(i)%> <%}%> still same erroe – RAna UzAir Sep 30 '17 at 17:01

2 Answers2

2

With some edits like declaring i you used in Scriptlets of form jsp could actually work but since I hear it's very not recommended I will offer another solution which is JSTL.

jsfile.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<title>Any title</title>
</head>
<body>
    <table>
        <tr>
            <c:forEach begin="0" end="${fn:length(completeTasks) - 1}" var="index">
                <td><c:out value="${completeTasks[index]}" /></td>
            </c:forEach>
        </tr>
    </table>
</body>
</html>

form.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!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>Form</title>
</head>
<body>
    <form
        action="<c:out value="${pageContext.servletContext.contextPath}" />/CompleteServlet"
        method="get">
        <input type="text" name="new-task" value="add new task" /> <input
            type="submit" value="Completed" />
    </form>
</body>
</html>

Sclass.java

import javax.servlet.http.*;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.*;

public class Sclass extends HttpServlet {

    private static final long serialVersionUID = 7806370535291118571L;

       public void init() throws ServletException {
          // Do required initialization
          System.out.println("init()");
       }

       public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          System.out.println("doGet() called");

          HttpSession session= request.getSession();
          String submittedTask = (String) request.getParameter("new-task");
          System.out.println(submittedTask);
          @SuppressWarnings("unchecked")
        ArrayList<String> arrOfCompleteTask = (ArrayList<String>) session.getAttribute("completeTasks");

          if (arrOfCompleteTask == null)
              arrOfCompleteTask = new ArrayList<>();

          System.out.println(arrOfCompleteTask.size());

          if (arrOfCompleteTask.size() >= 1)
          {
              for (int i = 0 ; i < arrOfCompleteTask.size(); ++i) {
                  System.out.println(arrOfCompleteTask.get(i));
              }
          }

          if (submittedTask != null) 
          {
              arrOfCompleteTask.add(submittedTask);
          }

          session.setAttribute("completeTasks", arrOfCompleteTask);

          request.getRequestDispatcher("/jfile.jsp").forward(request,response);
       }

       public void destroy() {
           System.out.println("destroy()");
       }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>TestServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
  <servlet-name>sname</servlet-name>
  <servlet-class>Sclass</servlet-class>
  </servlet>

  <servlet-mapping>
   <servlet-name>sname</servlet-name>
   <url-pattern>/CompleteServlet</url-pattern>
</servlet-mapping>


</web-app>
Anddo
  • 2,144
  • 1
  • 14
  • 33
0

You cannot do it like this, where is "i" declared even??:

<form action="/CompleteServlet" method="get">
<%String completeTasks=((ArrayList<String>)session.getAttribute("todoList")).get(i);%>
 <input type="hidden" name="completeTasks" value="<%=completeTasks%>" />
 <input type="submit" value="Completed">
</form>

Scriptlets (these things: <% %>), have been discouraged against since 2010. There's a whole list of reasons why you should avoid using them when you can.

What you should to do is this:

<form action="/CompleteServlet" method="get">
 <input type="hidden" name="completeTasks" value="${todoList}" />
 <input type="submit" value="Completed">
</form>

And then you either get the last value for completeTasks in CompleteServlet or set the last value before you send it in the form view. The latter being the simpler option. The former involves taking the completeTasks as a String and then making it an ArrayList by splitting each value, usually with a comma. With something like this:

String v=req.getParameter("completeTasks");
ArrayList<String> myList = new ArrayList<String>(Arrays.asList(v.split(",")));
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44