wood burning stoves 2.0*
The moose likes JSP and the fly likes JSP without scriptlets 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 "JSP without scriptlets" Watch "JSP without scriptlets" New topic
Author

JSP without scriptlets

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
hi...i find it really difficult to write jsp without scriplets...
do you really feel that we can live without scriptlets...

by the way whats the big harm in using them except that they are deprecated....

can everything can be done without them?
setting and getting request and session scoped attributes..for while loops....arrays...etc etc

are scriplets no longer needed...

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
well my company says that client needs a working software...he is not concerned if you use scriptlets or EL
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Yogesh Gandhi wrote:hi...i find it really difficult to write jsp without scriplets...
do you really feel that we can live without scriptlets...

Absolutely. I have not written a scriptlet in a JSP since 2002 when JSP 2 was introduced. if you cannot write your JSP without Scriptlet code, it's a sign that you are trying to do too much in the JSP that should be done in the page controller servlet.

by the way whats the big harm in using them except that they are deprecated....

They create a big old untestable mess. Please read this article.

can everything can be done without them?

Yes. That is, everything that should be done in a JSP. Remember, JSP is the view component. Control should be done in controllers, not views.

setting and getting request and session scoped attributes..for while loops....arrays...etc etc

The JSTL and EL handle all of that.

are scriplets no longer needed...

They are not needed, and haven't been for 10 years.

By the way, interrogative English sentences end with a question mark character (?) not an ellipsis (...).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Yogesh Gandhi wrote:well my company says that client needs a working software...he is not concerned if you use scriptlets or EL

That is a very naive attitude, in my opinion. Do you really think that "As long as it works, the code can be a mess and follow poor practices" is a proper professional attitude?
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Thanks Bear for reminding me that this topic already exists.

i almost forgot that ths topic exist, because I got no notification e-mails for responses on this and it almost skipped out of my mind that I had already created this thread.


I have read on internet that scriplets should not only be avoided, but any JSP should rather contain ABSOLUTELY NO JAVA CODE...

Is it really possible that every possible JSP that can be written with scriplet can also be written WITHOUT IT?

Lets say for Example

I have the following scriptlet
I am setting Header in response.
In Header, I am setting Pragma to no-cache and Cache-Control to no-cache

All this was being done in a scriplet..
Is there any way this can be done without it?
Also, i would be happy to know about a page, where in all the mappings from scriplet to JSTL are given, which can help me remove every possible scriplet in any JSP page.
I would like to learn the best practices and make it a habit


Thanks for all the replies. I will try to learn JSTL and EL the better way.. !!
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 207
Bear BibeaultThat is a very naive attitude, in my opinion. Do you really think that "As long as it works, the code can be a mess and follow poor practices" is a proper professional attitude?[/quote wrote:


No, I don't think so, that's why I am trying to learn JSTL and EL
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Yogesh Gandhi wrote:Is it really possible that every possible JSP that can be written with scriplet can also be written WITHOUT IT?

No, but it shouldn't be. With scriptlets you can do all sorts of inappropriate actions in a JSP that are not possible by design with JSTL and EL.

I haven't put any Java code into a JSP in over 10 years. And I write a lot of JSPs.

This assumes, of course, that one is not trying to do things in a JSP that don't belong in a JSP. If you find yourself trying to do something that's hard to do with the mechanisms that are available to scriptless JSPs, it's a major red flag that you are trying to do something inappropriate.

I am setting Header in response.

This is not an action that belongs in the view. Response headers should be set in the page controller.

In Header, I am setting Pragma to no-cache and Cache-Control to no-cache

Again, not a view action, but a controller action. Or even in a filter.

Also, i would be happy to know about a page, where in all the mappings from scriplet to JSTL are given, which can help me remove every possible scriplet in any JSP page.

No such thing is possible. You can do anything at all in Java scriptlets, but only view-oriented actions are appropriate in a JSP.

I would like to learn the best practices and make it a habit

Rules of thumb:
  • If it's not part of creating the HTML view, it doesn't belong in the JSP.
  • If you have to bend over backwards to do something using scriptless JSP mechanisms, it likely doesn't belong in the JSP.

  • It would be rare to be able to take a JSP that is full of scriptlets and just do a line-by-line conversion as the old JSP likely has lots of crap in it that doesn't belong there. Almost always a major refactor of the application is necessary. Which is why it's best to just do it right the first time.
    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    Well, I am trying to gather information from internet and I have created a POST : http://javakafunda.blogspot.com/2012/01/removing-scriptlets-from-jsp.html which would help people like me, to gain understanding on the alternatives available for scriplets...

    Right now I gathered only three of them, but I'll keep updating that article, but before that I believe I should gain mastery over JSTL and EL.

    One more doubt that was coming to my mind is

    there is logic:iterate and c:forEach

    Which should one use ? where ? why? and when?

    Do we have some sort of article where we can see the mapping of Standard JSTL tags to the corresponding struts tags...
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60748
        
      65

    If you're not using Struts, then obviously, no struts tags. if you are, then I'd prefer standard over proprietary --- but I wouldn't be using Struts in the first place.
    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    i am relaxed...need to master only jstl tags and not struts

    but for maintaining existing code one must be aware of both.

    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    guys...i want to have your opinion here...

    i add a record on the screen and submit...
    code flow goes like this

    jsp->action class->dao->jsp

    now in dao
    it sets
    1 if added
    2 if failed
    3 if record not allowed to add
    4 if record already exists
    and so on...


    as these status codes are not very user friendly...i suggested my team mate to define constants with a user friendly names in constant.java

    now in answering jsp

    earlier he was using logic:equal with a value attribute hardcoded. since we have removed hardcoding from dao, we should also remove it from jsp.

    but for that i had to put a expression like

    <% =
    Constants.RECORD_NOT_FOUND%>


    Can someone suggest wat are the alternatives available for such a requirement.

    my target is 0 scriptlet in jsp
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60748
        
      65

    Firstly, your flow:

    jsp->action class->dao->jsp

    does not follow modern conventions. The JSP should have a servlet page controller that does all the hard work of getting the page ready for display. usually this is the place to handle the complex logic so you don't have to worry about it in your page.

    That said, if it turns out that you do need access to a set of constants in a JSP, I wrote and article on that many years ago: this article
    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    thanks bear for the link. it was an interesting article about idempotent and non-idempotent oprations. though in first read i could only understand 50% of it...i need to read that again....but i was looking for the section which answers my question. but i was unable to find answer to my question..
    do you think it'll be good to create a separate jsp for each response....just to avoid scriptlets....???
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60748
        
      65

    Yogesh Gandhi wrote:do you think it'll be good to create a separate jsp for each response....just to avoid scriptlets....???

    I do not know what you mean by this or what it has to do with the question of constants access.
    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    okay...let me put it this way...

    what i understood from th

    e article was...

    one possible solution can be to create one jsp per case like

    one for successfully added record
    second for record alrady exists.
    third for invalid data
    fourth for permssion denied

    then instead of checking the return value in jsp...we check it in action class and fwd to to corresponding jsp.


    this way we'll not have scriptlets in jsp..

    but with this approch it looks lyk too much of work...
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60748
        
      65

    I have no idea how you got that from any of the articles that I pointed you to.

    A JSP can be as dynamic as you like, just by using the JSTL and EL, which are very flexible, and avoiding Java code. Anything that requires Java should be handled in the page controller.
    Yogesh Gandhi
    Ranch Hand

    Joined: Dec 05, 2008
    Posts: 207
    no no no not from your article...but while reading your articlr a random thought came to my mind...:)

    By the way, what do you suggest for accessing constants in jsp..?? hope i m not bothering you much...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JSP without scriptlets
     
    Similar Threads
    exception thrown from the servlet service function
    JSP not picking up getAttribute from Servlet
    JSP
    Using JSTL in scriplets - compile error
    Scriptlets