GeeCON Prague 2014*
The moose likes JSP and the fly likes JSP Coding Conventions/Best Practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Coding Conventions/Best Practices" Watch "JSP Coding Conventions/Best Practices" New topic
Author

JSP Coding Conventions/Best Practices

Linda Walters
Ranch Hand

Joined: Apr 30, 2005
Posts: 100
Howdy,

As part of a project that I am working on I need to either find or develop coding conventions and best practices for developing JSP. So far a Google search for such has been largely unsuccessful. Are there any industry recognized coding conventions and best practices for JSP and if so, where can I find them?
[ April 10, 2006: Message edited by: Linda Walters ]

<a href="http://labryssystems.net/pblog/index.php" target="_blank" rel="nofollow">Javaville Gazette</a><br />Non-cooperation with evil is a duty. -- Mahatma Gandhi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Modern best-practices are leaning towards scriptless JSP pages that employ the JSTL and EL, along with custom actions (tags), to replace what used to be done with scriptlets. Without any Java code on the pages, coding conventions become a simple matter of dictating how the EL should be used to reference scoped variables.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I've also read that only request attributes should be used by JSP.
There's a whole chapter about JSP/JSTL usage in J2EE Design and Architecture.
You might be interested in it.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by Satou kurinosuke:
I've also read that only request attributes should be used by JSP.


Huh? I don't know where you read that but it makes no sense. Page context is an exteremly valuable location for scoped variables, especially when using the JSTL, and each of session and application scopes are essential to well-structured applications.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Sorry. I was referring to "Expert One-On-One J2Ee Design and Development".
I remember that jsp views should only use the needed data from the request scope. It is not the role of views to deal with scope attributes.
I read this a long time ago I'll give it another shot tonight.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Found it, I didn't dream

It says, p537 :
"Views shouldn't be given access to session data, as they might modify it, subverting good design."

Another one in the JavaBeans section:
"There are four values for scope : page, request, session and application, of which only one (request) is compatible with correct use of JSP in an MVC web application, as views shouldn't access (and potentially manipulate) session or application-wide state."
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Professional JSP 2nd Editionhas chapter named Maintainability and Good Practice - read it. I can't recall the content.


My blood is tested +ve for Java.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Here is one of the good article to start with:
Best Practices to improve Performance in JSP
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Even the following site provide some good practices:

http://www.javapractices.com/TableOfContents.cjp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by Satou kurinosuke:
Found it, I didn't dream

It says, p537 :
"Views shouldn't be given access to session data, as they might modify it, subverting good design."

Another one in the JavaBeans section:
"There are four values for scope : page, request, session and application, of which only one (request) is compatible with correct use of JSP in an MVC web application, as views shouldn't access (and potentially manipulate) session or application-wide state."


I think that that is all complete nonsense. Just because you read something in a book doesn't always make it gospel.

While I can agree that views should generally be idempotent, pretending the other scopes do not exist "because they might accidentally modify them" is patently ridiculous. One might as well say "don't write any code because you might introduce a bug".
[ April 11, 2006: Message edited by: Bear Bibeault ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I presume if you did a google search you did find JSP1.x code conventions?
It would be a good place to start, even if it is a little out of date.

Other things I might mention:
${user.name} vs <cut value="${user.name}"/>
Even though a JSP2.0 container will recognise an EL expression on its own, still use the <cut> tag as it escapes undesirable HTML characters.

Specifying access scope of variables in EL
${user} is equivalent to pageContext.findAttribute("user") which searches through the scopes page, request, session, application in that order, using the first it finds.
${sessionScope.user} specifies the "user" attribute in session scope specifically.
It can be argued you should always specify the scope of the attribute, both for documenting where you expect it to be, and preventing potential errors from the same attribute name being used in multiple scopes.

Cheers,
evnafets
[ April 11, 2006: Message edited by: Stefan Evans ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Just because you read something in a book doesn't always make it gospel.

I agree. I thought I'd read that somewhere, that's all.
Somebody is asking about good practices in JSP, and I 'kindly' gives him some information. It could help to have different sources.
I'd argue about saying that it is "complete nonsense", but you're the boss

PS : are you angry or something ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by Satou kurinosuke:
I'd argue about saying that it is "complete nonsense", but you're the boss


Actually, no I'm not. When chiding someone for naming convention violations, or being not nice, then I'm the boss.

When answering questions, I'm just a contributor like anyone else. So if you disagree with me, feel free to say so. I would however, ask that if we are going to discuss the merits of scoped variable usage in JSPs, that another thread be started. We've already hi-jacked Linda's poor thread pretty badly.

are you angry or something ?


Of course not. Though some people have said that I tend to "sound" angry when writing... but be assured that it is not so.

Perhaps it's because I am outspoken when I hear what I think is rubbish. But angry? No.
[ April 11, 2006: Message edited by: Bear Bibeault ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Though some people have said that I tend to "sound" angry when writing...

funny, I've been told the same
Thanks for the reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Coding Conventions/Best Practices