Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is putting Java code inside JSP encouraged?

Good evening,

I am currently in the progress of getting a degree in Programming at an academic institution (not a University) in Germany. We also do web development with Java ee there. This particular course started with using Servlets and progressed to JSP. Using servlets to handle business logic and then printing those results with jsp and using some of the basic things provided by jsps (looping over collections e.g.) seemed to make sense. But recently we dove deeper into the world of JSP and did scriptlets and similar things which boiled down to putting more and more business logic into the jsp file and ditching servlets altogether. And this entanglement of Java code and business logic and the praise of doing this is somewhat beyond me. I always thought one of the main goals of web application development was to have the main business logic separated from frontend matters (a thing which django and its template language is doing very well imho).

I find this thought somewhat mind-boggling that on one hand they teach us to keep loose coupling in mind when coding in one subject and in the other subject we are being taught to move more and more business logic into the templates. What even bothers me more so, is that if one googles some solutions to java ee problems, a high number of results shows solutions where lots of logic is happening in a template file, somewhat confirming that this mixing of template and programming language seems to be an accepted way of doing things in the ee world / encourages aspiring developers to adopt such practices.

Now from what I've heard, java for the web doesn't seem to be as big as a thing anymore, and if you look at the most popular webapps, hardly any of those are implemented in java, yet this aforementioned aspect always amazes me.

So the concrete questions here would be why is this high amount of coupling between template and business logic considered good practice in java ee?

Greetings, derelektrischemoench

like image 328
derelektrischemoench Avatar asked Sep 20 '26 15:09

derelektrischemoench


1 Answers

Actually, it is not a good practice. I think that lots of code you can find on Internet was written in wrong way because of various reason: probably it was developed to test some functionality and not to be deployed in a production environment, without evaluation of loose coupling, quality issues et cetera. Moreover, for various problems I always have to search on Internet. Most of time I find the solution and it lacks of class coupling, unsafe methods and so on. The point is: do not take this code you can find on Internet as an example of how something is done. Just use this code as a suggestion of how you can solve the problem you have and apply an improved version of that code in your production code. This applies not only to JSP or Java, but more generally to every kind of code you can read. Always remember that the code you have found somewhere with the help of Google was probably a fast "trial and errors" driven code that will never go on production and never will be changed. Your work as a developer is not copying-and-pasting that code, but organizing that code in the most maintainable way possible. I encorauge you to take a look at SOLID principles. To me SOLID principles enforces decoupling and other aspects that helps writing better code, and it is very important when you write a real-world product, because probably you are going to change it lots of time in future. Internet examples are not designed to be improved, just to be quickly understood.

like image 180
Lorelorelore Avatar answered Sep 22 '26 03:09

Lorelorelore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!