2

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++;
}
11
  • 4
    Could you please make a better attempt at making the question? Commented Apr 23, 2013 at 12:15
  • seems like your question is unrelated to JSP. Please include any related code and rephrase your question Commented Apr 23, 2013 at 12:17
  • i have one java file in which i try to get data from mySql table and store array "myArray" using Resultset. after this step i want to print "myArray" to web page using jsp file Commented Apr 23, 2013 at 12:19
  • how to print array on web page using jsp file Commented Apr 23, 2013 at 12:21
  • 3
    Avoid scriptlets, use EL or tags! Commented Apr 23, 2013 at 12:25

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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++;
}
%>

8 Comments

shoould i import any thing in my jsp page?
Not more than what you would import for that piece of code anyway.
@Rajan import java.sql.ResultSet
i just copy above jsp code and past in to my jsp page. it give me error
@navand , your point is correct, but to do all these , surely you need to import the java class
|
2

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
 %>

10 Comments

what is printMe() is this method of java calss?
just a method in java class,like public String[] printMe() here, this method will return string array
<%@ include file="/html/testi/init.jsp" %> <%@page import="com.testi.TestiPortlet"%> TestiPortlet obj=new TestiPortlet(); String str[]=obj.ordering(actionRequest, actionResponse); out.println(str[0]); %>
If the method is ordering() is string array, the following code is correct, you can go ahead, <%@ include file="/html/testi/init.jsp" %> <%@page import="com.testi.TestiPortlet"%> <%TestiPortlet obj=new TestiPortlet(); String str[]=obj.ordering(actionRequest, actionResponse); out.println(str[0]); %>
@kark : usage of scriptlet in JSPs are discouraged. try to use JSTL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.