Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP PROBLEM  RSS feed

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside my JSP , I am executing a query on the database and displaying results of the query in
a list box.
Now, I have moved the business logic ( i.e executing the query and filling up the list box )
to a separate Java class.
What I am doing inside the Java class is create a StringBuffer and append the HTML list box to it
and inside my main JSP I am simply printing out the StringBuffer using out,println
The JSP when executed shows a empty list box but no contents.
This is what I am doing inside the Java class for short
public class HTMLSQLResultSet {
public String toString() {
if ( count > 0 ) {
stringBuffer.append("<P><SELECT name='selectedPage' onChange='changePage(this.form.selectedPage)' ondblclick='handleMe(this.form.selectedPage)' size='5' text='00008B' bgcolor='0000FF' >");
}

for ( int i = 0; i < count; i++ ) {
stringBuffer.append("<option value = '<%= records[i][0] %>' > <%= records[i][1] %> <%= records[i][2] %> <%= records[i][3] %>");
}

return stringBuffer.toString();
}

}
In the main JSP, I am simply saying

HTMLSQLResultSet result = new HTMLResultSet(....................);
out.println(result);
Here , I have shown the partial code for sake of brevity.
Hope , I am able to convey the problem
 
Raj Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your approach is perfect but it seems you are missing something.
Here is what you should do:
In your Class file there should be a method which returns a string value. This is the string value which you want to print in the jsp page.
public String HTMLText(){
// here you generate the HTML Text you want to print

return HTMLText;
}
Suppose the name of your class file is OptionValues
Then in you jsp page you should code
<%OptionValues op = new OptionValues()
out.write(op.HTMLText());%>
This will put the returned string in the stream.
Hope it helps.
Raj

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!