wood burning stoves 2.0*
The moose likes JSP and the fly likes Insert text into JSP output with java bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Insert text into JSP output with java bean" Watch "Insert text into JSP output with java bean" New topic
Author

Insert text into JSP output with java bean

Johannes Salter
Greenhorn

Joined: May 02, 2010
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60774
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Johannes Salter
Greenhorn

Joined: May 02, 2010
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60774
    
  65

Your method can return anything. The number of elements doesn't matter. It's all just text.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: May 02, 2010
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60774
    
  65

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

Joined: Sep 29, 2008
Posts: 12617

Johannes Salter wrote:Can I return a list of Strings as text?

Sure, why not?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert text into JSP output with java bean
 
Similar Threads
while-loop
Mock exam questions
sort a head
JavaScript code question
output newline to CSV file how to