• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java code in JSP  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.....
why it is bad idea to put Java code in Jsp?
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it makes it difficult to read/debug especially when something goes wrong. The availabily of JSTL has made it a whole lot easier to achieve the same thing.
 
Jason Cohen
Smart Bear Support
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debugging isn't too hard with modern IDE's that link up errors with line numbers etc..

The real reason is that it breaks the separation between "view" logic and "application" logic.

Specifically, the "view" is how the user sees information: Styles, text, layout, art, pop-ups, etc.. This is stuff that can change easily, and does change often especially at the start of an application's life. These are things that non-programmers often have a finger in -- tech writers for the text, designers for the art, translators for the text.

In contrast, application logic is how things actually work: talking to the database, parsing out the "first" and "last" name out of a single "full name" field, validating input, processing data. These are things that non-programmers will not have a place in. This is where most of the bugs come from and ALL of the hard bugs.

Furthermore, application logic should come with lots of unit tests. View code can be hard (sometimes impossible or not worth the effort) to test.

There are more differences, but just as with object-oriented programming it's useful to separate layers, these are two layers that should be separate. So think of JSP as "just display stuff" and the rest as "just application logic" and your application will be better organized.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this article helpful.
 
Alex Pustovit
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just 1 thing to add: if you do need complicated logic in your JSP to render some info, try to consider using of custom tags.
And your non-java-programmers will be happy with lack of java code in JSP.

The J2EE Tutorial - Custom Tags in JSP pages
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!