Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Code Generating HTML  RSS feed

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, when dealing with Ajax scenarios, I can't help but feel I've taken 2 steps back with server side programming. Long gone have been the days of servlets spitting out HTML within a huge list of out.write statements. Or are they?

Up to this point, I've tried to handle HTML generation on the client. Letting Javascript manipulate the DOM as much as humanly possible(readable). But more and more lately, I've been inclined to generate HTML in Java code. Not specifically in a servlet. I have a few helper classes that do this for me. Then I use prototype's Ajax.Updater function to just pop this HTML in a div for me.

To me, that is much cleaner than Javascript. Obviously, this doesn't work well for every case. There are some situations where only a row in a table needs updated, and it's better to let Javascript handle this rather than drawing the entire table all over again.

What I am wondering is, am I breaking any coding laws by doing this? If Ajax weren't in the equation, I'd get smacked down for doing such things rather than using JSP's. The more things change, the more they stay the same, or is it history repeating itself?

What say you?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, that is much cleaner than Javascript.

Why ?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:

Why ?


Because I know java better than Javascript probably. Which is why I preceded that statement with "To me...". It also puts the burden on the server rather than the client. Having a lot of javascript running on the client can slow things down when rendering in the browser.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you're breaking laws here, we're all programming outlaws after all But what happens if the guy doing the server-side and the guy doing the client-side are actually two guys ? The client-side guy is expecting some particular HTML code to come from in the response. Both guys might become pretty nervous when one is expecting something, while the other is doing something different

The server-side guy will also have to be aware of the layout of the page, and know HTML too. Which is not always the case.

Moreover, putting a bit of HTML here and there, sounds like pretty difficult to maintain.

For a small 'one guy' application, it may be fine, but I'm not sure it would be ok for something bigger.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Ajax weren't in the equation, I'd get smacked down for doing such things rather than using JSP's.


There's nothing in Ajax that prevents you from using JSP to format HTML fragments. Why not use JSP? I do it all the time.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


There's nothing in Ajax that prevents you from using JSP to format HTML fragments. Why not use JSP? I do it all the time.


How do you mean Bear?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not have experience in UI related stuffs but I have worked quite a bit in javascript and AJAX.Giving my best shot.

I do not consider any laws while programming (except for the coding standards).So I do not think that by doing anything we can break any law.

Can we not produce the complete view using jsp.Still we can manupulate at client side using AJAX.
Generating pieces of code from java helper classes might be helpful in case you have component base architecture , like every HTML component would have a helper class to generate the html code and then according to the design of the UI , you can put those individual compoenents in a layout of page and use control the components using AJAX at the client side.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new Eclipse Review has an article on "Ajax Meets JSF" that seems to be right up this alley.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:


How do you mean Bear?


From the point of view of the server, whether the request is an Ajax request or not is moot. You can use JSP just as you would with any other request to format the HTML to be returned as the response to an Ajax request.

Why do you feel that you cannot?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


From the point of view of the server, whether the request is an Ajax request or not is moot. You can use JSP just as you would with any other request to format the HTML to be returned as the response to an Ajax request.

Why do you feel that you cannot?


Yea, I was thinking about that. I'll give some of that a go and see how it works with what we are needing to do. Thanks Bear.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for AHAH as well. The As are the same as AJAX but with HTML instead of the JavaScript and XML. The count on Sesame Street loves it.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


From the point of view of the server, whether the request is an Ajax request or not is moot. You can use JSP just as you would with any other request to format the HTML to be returned as the response to an Ajax request.

Why do you feel that you cannot?


Just an update to this. I was finally able to get around to messing with this and using the JSP rather than HTML in Java code is a great solution. In fact, it's even tons better than trying to manipulate the DOM in most cases.

Thanks for the advice Bear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!