permaculture playing cards*
The moose likes JSP and the fly likes StringBuilder vs other java Objects 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 "StringBuilder vs other java Objects" Watch "StringBuilder vs other java Objects" New topic
Author

StringBuilder vs other java Objects

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
Hi,

I did the code using stringbuilders which are not as efficient as using some Java objects on the session and a JSP include to generate the html. I need do it more efficient. Which java objects should I use? Any idea, please?

Regards

Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
Angus Ferguson wrote:I did the code using stringbuilders which are not as efficient as using some Java objects

What makes you think that ? Did you profile your application or did you read it somewhere ? If the latter, did they suggest alternatives ?

It all depends on what you are doing with the StringBuilders.
At the moment your question is like asking - "At the moment I'm walking. What can I do that is more efficient ?"
If you're visiting a neighbour, then walking is probably the most efficient. If you want to get to the other side of the world, then taking a plane might be a good idea.


Joanne
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
Hi,

I am creating HTML in the java class like



but I want use some Java objects on the session and a JSP include to generate the html in order to improve performance

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

While I believe that building up HTML in strings is a really poor practice -- HTML as template text is why JSPs exist int he first place -- what makes you think the alternative approach will be any more efficient? Are you just guessing?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7494
    
  18

Angus Ferguson wrote:but I want use some Java objects on the session and a JSP include to generate the html in order to improve performance...

Joanne's absolutely right: you've got this bass-ackwards.

1. Does your program work? Don't even think about efficiency until you can say 'yes'.
2. Is your program running too slowly? If not: see 1 (and you'd better have some good metrics to back it up).
3. Do you KNOW that the reason it's running slowly is because of your StringBuilder? If not: again, see 1.

Basic lesson: Don't worry about efficiency until you have a well-designed, and well-written, working solution.

W.A. Wulf wrote:More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 331
Well

I mean don't generate the HTML in the Java, use a JSP to do that based on an object.

What I meant is create an object based on the back end and stick it in the session after that retrieve the object in the session from the .JSP. Is that correct for you?

Use the object from the session and a JSP to generate the html. Then you only create the object once per session and the slow string manipulation is done by tomcat which is optimised for that kind of work

What way you would do that?

Thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7494
    
  18

Angus Ferguson wrote:and the slow string manipulation is done by tomcat which is optimised for that kind of work

Again, you still seem to be assuming that it's the string manipulation that's slow. Do you have any proof?

That said, it's certainly better to only have something done once if you can; and in a client/server environment, that generally means doing it at the server end.

However, we (or at least I) still have no idea what your actual problem is; and in the absence of other information, my first port-of-call would be your network pipeline. You can make all sorts of things server-centric, but if you don't have the network to support it, no amount of Java-based efficient code is going to change things.

Winston
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Yes, you should be creating an object/bean in the controller (or a delegate such as the model), and placing it one of request or session scopes in order to generate the HTML in the JSP. Prefer request scope unless you have a really really good reason to put it in session scope.

But you do this because it's the right way to do it; not because it may (or even may not) have some infinitesimal performance gain.

As said before, worry about performance when and if a performance problem raises its head. Always code for clarity; not supposed performance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuilder vs other java Objects
 
Similar Threads
java program as a COM Server?
ImageIO.read() timeout?
Immutable classes in java
Efficient way to compare complex objects?
Any Other Techniques other than httpSession