Kapil
Kapil

Reputation: 350

how to print java String Array in jsp page.?

Hello I am new in jsp i want to print my String array of Java file to My jsp page how to print in web page tell me .. i dont know how to do this.

while(rs.next()){
    count++;            
    anArray[i]=rs.getString("subject");         
    System.out.println(anArray[i]);
    i++;    
}
while(rs1.next()){   
    anArray[i]=rs1.getString("subject");            
    System.out.println(anArray[i]);         
    i++;
}

Upvotes: 2

Views: 21175

Answers (3)

kark
kark

Reputation: 4853

As i understand your, you are making the output in console , and you are trying to print it in browser.

To get the output in browser

Just import the java class in JSP page

In JSP page between tag(<%..%>) by java class object print it in browser,

<%@page import="pack.sample"%>
<%
//In scriplets
Sample obj=new Sample();
String str[]=obj.printMe();//Here printMe() is a fn from Sample class which will return string array

//Now here do all stuffs with str[]
//out.println(str[0]);//It will return zeroth value of str[] in your browser
 %>

Upvotes: 2

CodeChimp
CodeChimp

Reputation: 8154

Assuming what you provided is an example of what you want to do in your JSP, the easiest way to do what you are tying to do is to use a JSTL forEach.

<c:forEach items="${yourArray}" var="myItem" varStatus="myItemStat">
  yourArray[${myItemStat.index}] = ${myItem}
</c:forEach>

This assumes you have passed your "yourArray" to the JSP. There are LOTS of tutorials on how to do all of this sprinkled all over the Internet.

Upvotes: 4

Navand
Navand

Reputation: 476

In jsp page you can use this:

<%  
while(rs.next()){
    count++;            
    anArray[i]=rs.getString("subject");         
    out.println(anArray[i]);
    i++;    
}
while(rs1.next()){   
    anArray[i]=rs1.getString("subject");            
    out.println(anArray[i]);         
    i++;
}
%>

Upvotes: 3

Related Questions