aspose file tools*
The moose likes JSP and the fly likes Is this good practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Is this good practice?" Watch "Is this good practice?" New topic
Author

Is this good practice?

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
On the signin page:



Is it a good approach?

Garrett


comp.lang.javascript FAQ: http://jibbering.com/faq/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

I would take issue with this approach on a number of dimensions;

1) Use of Java scriptlets. I'd recommend JSTL instead.

2) Hard-coded error message on page. Better to lookup message text from a property file.

3) Using request param. I'm assuming the page is forwarded to from a servlet that did the authentication check? Scoped variables are a better communication mechanism.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Bear Bibeault:

2) Hard-coded error message on page. Better to lookup message text from a property file.

While I don't disagree with the other two points, I'm skeptical of this one. It looks to me like this error message is meant for a user, not a programmer. It's thus part of the content for the page, and I think it's reasonable to keep it where the HTML would be, in the page.

An alternate approach is to keep all content separate from the HTML, but I don't think that is the only good approach.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
agreed upon looking the message text from property file. no matter the message is for user or programmer or someone else.
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Thank you.
ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
Hi Garrett,

When coding JSP/ASP/CFM/PHP, here is how I structure my page;

1. At the top I decleare my variables that I'm going to use including two called String errMsg = "" and another one called boolean isValid = true;

2. Following from there, I begin doing my businesss logic. Note that no HTML ouput yet. In a JSP this is where i would have the line:
if (request.getParameter("signin_error_code") != null){
errMsg = "Signing error..blah blah blah";
isValid = false;
}

then..

if( isValid ) { //do more business logic, until you have validated evrything that you need validating...then finish off by inserting records into database or sending an email message }

//The idea is that you do your business logic incrementally, i.e you do not go to the next step unless the previous step has passed the rules of your business domain.

//Done business logic
redirect somewhere or set some flags to determine which chunks of HTML code to display.

Begin HTML section

Somewhere here you display the errorMsg if !isValid using an expression. This is **only here ** where you display your error msg.

End HTML section

######################################################
Concerning storing messages in properties file: I wouldn't go that far for a simple application. This type of setup becomes necessary when you start using frameworks such as Struts for building complex web apps since you can internationalise your app using the message bundles (prop files). I hope that helps...a bit
[ October 23, 2004: Message edited by: ernest fakudze ]

In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Since this thread asks about best practices, I must point out that:

Following from there, I begin doing my businesss logic.


Placing business logic within the JSP page is considered a very poor practice. Pleae do not fall into this trap.
[ October 23, 2004: Message edited by: Bear Bibeault ]
ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
Hi Bear,

Sorry, I didn't make myselft clear, here is what I meant: In the business logic section is where I instantiate Java classes that I've built and validate the form data against. I'm aware that decoupling the *core* business logic from the views (JSP) is better since it makes the app more manageable and brings about the possibilities of code reuse. If I'm still missing something, please let me know, it's good to go with best practics.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is this good practice?