# Java Web Devel - MVC RequestDispatcher - Passing Arrays?

**URL:** <https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448>\
**Category:** Factual Questions\
**Created:** [October 14, 2006, 7:24pm UTC](https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448 "2006-10-14T19:24:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [October 14, 2006, 7:24pm UTC](https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448/1 "2006-10-14T19:24:04Z")

</div>

Dopers,

I’m brushing up on some Java web development and read about implementing an app with MVC using the Request Dispatcher. I coded together a quick example, passing strings and handing them off to a JSP page to display. I’m stumped, though, with how to pass arrays.

In my Servlet:

```auto

public class Action extends HttpServlet {
	
	static final long serialVersionUID = 1;	
  
	public void doGet(HttpServletRequest req,
                    HttpServletResponse res)
    	throws ServletException, IOException
    	{

		 String months[] = 
	       {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
	        "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
		
	    req.setAttribute("months", months);
	
	    ServletContext app = getServletContext();
	
	    RequestDispatcher disp;
	    disp = app.getRequestDispatcher("/view/resize.jsp");
	
	    disp.forward(req, res);
    	}
}

```

My jsp:

```auto

<html>
<head>
<title>Test Page</title>
</head>
<%String[] months = session.getAttribute("months");%>
<body bgcolor='white'>
<%for(int i = 0; i < months.length; i++ ){%>
	<%=months*%>
<%}%>
</body>
</html>

```

This returns this error:

```auto

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 4 in the jsp file: /view/resize.jsp
Generated servlet error:
/opt/tomcat5/work/Catalina/localhost/flowWeb/org/apache/jsp/view/resize_jsp.java:45: incompatible types
found : java.lang.Object
required: java.lang.String[]
String[] months = session.getAttribute("months");

```

I’m using Gentoo Linux with Sun’s JDK 5 and Tomcat5.

Thanks,

LarsenMTL

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 14, 2006, 7:28pm UTC](https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448/2 "2006-10-14T19:28:58Z")

</div>

This is really just a guess, but have you tried this?

```auto

req.setAttribute("months", {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"});

```

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 14, 2006, 7:37pm UTC](https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448/3 "2006-10-14T19:37:15Z")

</div>

Strike that. My better guess is that req is set up to store attributes of any type, and so it just stores them as objects. So when you get an attribute, it just comes back as an object and you have to do something to get that into an object of the right type. You’re either going to have to cast the object you get back into a String, or write a more type-specific get/set pair.

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [October 15, 2006, 2:55am UTC](https://boards.straightdope.com/t/java-web-devel-mvc-requestdispatcher-passing-arrays/376448/4 "2006-10-15T02:55:27Z")

</div>

First I had an error in my jsp, session.getAttribute(“months”); should be request.getAttribute(“months”);.

Regardless,

[QUOTE=ultrafilter]  
You’re either going to have to cast the object you get back into a String, or write a more type-specific get/set pair.  
[/QUOTE]

was dead on. Casting like so –\>

```auto

<%String months[] = (String[])request.getAttribute("months");%>

```

worked. Thanks **ultrafilter.**
