Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to pass array from servlet to jsp

800603Aug 10 2008 — edited Aug 21 2008
Hi,

I want to pass int and string array form an servlet to jsp. I retrieve the data from form database code and store it in the strings. The string arrays are not empty ( I checked it by directly running servlet in browser and printing the values by using PrintWriter ).

I'm using session.setAttribute() in servlet and session.getAttribute() in jsp below is the code ,

servlet code
/*

DataBase code


*/

   public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
		{
				res.setContentType("text/html");
				PrintWriter out = res.getWriter();
				String ge=req.getParameter("CMD");
				HttpSession session=req.getSession(true);
				if (ge.equals("geQ"))
				{		
						
					/*     int c2=0;
						while (c2<5)
						{
								out.println(Q[c2]+"<br>");
								
								c2++;
						}     */
				}
				
				session.setAttribute("Q",Q); // Q is string
                                                                
				
		
		}

		
Jsp code
<%@ page session="true"%>

     <body onload=http://localhost:8080/OLT/GeQuest?CMD=geQ>   // to call the servlet when page loads to generate DataBase Query  

<%!String[] srt=(String)session.getAttribute("Q");

%>
	
	
	<%while(i<5)
{%>
	
	<%=srt%>

<%
i++;
}%>


</body>
 

But I'm getting Error : session cannot be resolved

I'm not sure that this is a proper way to do it. Please help me out if you have any suggestions for this problem.


Thanks in advance,
Harsh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 18 2008
Added on Aug 10 2008
3 comments
1,614 views