JSP and scriptlets

2019-06-19 01:47发布

问题:

I know that using scriptlets are considered taboo nowadays. Its okay and I will agree to the Top Star's words (as I am just a novice in Java at the moment).

What I have heard so far is, its to make the designers life easier. But I wonder, if it has anything to do with performance of JSP pages. On the other hand, if its just for "making designers life easier", what do you guys think about using scriptlets in a situation where Java developer do both the work ?

If scriptlets are bad in all aspects nowadays, what are your recommendations ? EL ? JSTL ?

Thanks

回答1:

It is not just about making others life easier, it is also about making YOUR life easier. JSTL and other tags force/help you to write a JSP properly. A problem with scriptlets is that people do things in a JSP that they shouldn't. I mean, JSTL (among other tags) helps you to keep MVC pattern because basically, if there is something that you cannot do with JSTL in a JSP, it is because you should not. If you do not respect MVC and do things in JSPs that you shouldn't

You can use JSTL, struts tags, springs tags, etc. There are few options and you can combine them.



回答2:

EL and JSTL is what I recommend because they make developers life easy too. Your code readability is improved when you use EL and JSTL in comparison to scriplets. With JSTL, I experienced that iterating through an arraylist is so easy in comparison to using scriptlets. This is just one thing that I found so useful. But there are plenty of it.



回答3:

I personally don't feel scriptlets are bad, if you are doing small condition checks like is session valid, or user has logged in etc like that. And if Java developer is also a UI developer then for small codes it should be ok. But doing lot of java things like fetching data from DB, business rules validations etc is a very very bad. Though I strongly recommend EL and JSTL, scriptlets are ok for few lines of code incase you don't have time to learn JSTL (like project deadlines etc)



回答4:

Scriptlets are definitely something not recommended as of now. The primary reason being handling special cases all over for NullPointerExceptions, etc. Refer: Head First Servlets and JPS. Expression Languages are a refined version of the same, plus providing lot more functionalities. Always remember, you must always avoid scripting in your JSPs.

Using expression languages and JSTLs just not make your code clean, but also help developers avoid unnecessary handling many a times.