is it possible that i could create an array from a class and return the value of that array? and how? how can i access that array

Dani AI

Generated

A quick diagnosis for : returning an array from a plain Java class is fine (as showed), but when you move to a web app you must make sure the populated array (or collection) is placed into a JSP-accessible scope and that the JSP is reached with the same request. Null on the JSP nearly always comes from one of these causes: the array was never populated, the attribute name/scope is wrong, you used a redirect (which loses request attributes) instead of a forward, or the array is stored in a shared servlet field and overwritten by another request.

Checklist to debug:

  • Verify the array/collection is non-null and populated immediately before you set it as an attribute.
  • Use request.setAttribute("name", value) and then forward to the JSP. Don’t redirect if you expect the same request attributes.
  • Match the attribute name exactly in the JSP.
  • Prefer a List<T> or T[] of object wrappers (e.g., Integer[]) for smoother JSTL/EL handling.
  • Avoid storing per-request data as servlet instance fields (thread-safety).

Example servlet pattern (minimal):

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    List<String> rows = dao.fetchRows(); // must be non-null
    req.setAttribute("rows", rows);
    req.getRequestDispatcher("/WEB-INF/results.jsp").forward(req, resp);
}

JSP iteration via JSTL:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:forEach var="r" items="${rows}">
  ${r}<br/>
</c:forEach>

Quick tips: print/log the attribute in the servlet before forwarding to confirm it’s populated; if you use a bean, ensure it’s placed in the same scope the JSP expects; and avoid redirects when passing request attributes.

Recommended Answers

All 3 Replies

Yes its possible to create an array in a class and multiple ways to return it depending on what your trying to do, So what are you trying to do?

for example, i am accessing values from a database.. i want to store them in an array.. but my problem is i can't access my array on my jsp page..
it returns null.. my program is quite long so i hope you could help me with a simple program so that i can pattern your program with mine. thanks really..

Here is some simple code that will show you how to access private variables in a class, If this is not what your looking for let me know.

public class myClass{

      private int[]  myArray = new int[10];

     //return value of specified index      

      public int value(int indexNumber)
     {
         return myArray[indexNumber]
     }

     //returns the array to the calling object

     public int[] getArray()
    {
         return myArray;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.