Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert text into JSP output with java bean

 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and howdy!

I have a problem in output handling with JSPs.

I want to have an output like:
Text 1
Text 2
Text 3
Text 4

Whereas "Text 1" and "4" come from the JSPand "Text 2" and "3" from a bean.

myText.jsp:myBean.java:The problem is, I get:
Text 2
Text 3
Text 1
Text 4

So the text is always added at the start of the JSP HTML-Code and not within, from where the method is called.

How can I solve this problem?

Is there a more elegant way to solve the problem?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, your JSP is using a lot of old technology. Scriptlets have been discredited for over 8 years now. You should update your JSP knowledge to embrace the JSTL and EL instead.

That said, the problem is that you are trying to emit output via scriptlets rather than scriptlet expressions. If you are going to continue to use scriptlet technology (not recommended), use scriptlet expressions (&using lt;%= %>) rather than scriptlets.
 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, I have a lot of code in my real bean. I don't want to integrate all this Code into my JSP.

I just need to output one or two lines of HTML-code from my bean at the right position.

Expression like <%= don't work here, since I have a variable number of output elements.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method can return anything. The number of elements doesn't matter. It's all just text.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johannes Salter wrote:The problem is, I have a lot of code in my real bean. I don't want to integrate all this Code into my JSP.

And you don't need to; sounds like it should be a custom tag instead.
 
Johannes Salter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Your method can return anything. The number of elements doesn't matter. It's all just text.

Can I return a list of Strings as text?

PS: First, I want to learn basic JSP and servlet handling. Then I continue with custom tags...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just learning, and not supporting a legacy application, why are you using scriptlets rather than doing things the proper way?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johannes Salter wrote:Can I return a list of Strings as text?

Sure, why not?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic