Java – what is the point of beans?
I have some JSP tutorials for beans. I don't understand what a bean class is All is to get and set methods Why use them?
public class UserData { String username; String email; int age; public void setUsername( String value ) { username = value; } public void setEmail( String value ) { email = value; } public void setAge( int value ) { age = value; } public String getUsername() { return username; } public String getEmail() { return email; } public int getAge() { return age; }
}
And JSP are
<jsp:useBean id="user" class="user.UserData" scope="session"/> <HTML> <BODY> You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>
Solution
>Bean gets all the benefits of Java's "write once, everywhere" paradigm
> Advantages of Bean
More than a decade ago, using scriptlets (these <%% > things) was really discouraged because of the birth of taglibs (such as JSTL) and EL (expression language, those ${}) The main disadvantages of scriptlet are:
>Reusability: you cannot reuse scriptlets Replaceability: you cannot abstract scriptlets. > OO capability: you cannot use inheritance / composition. > Debuggability: if the scriptlet throws an exception halfway, all you get is a blank page. > Testability: scriptlets are not unit tested. > Maintainability: it takes more time to maintain mixed / chaotic / repetitive code logic
> Check Whole…BalusC’s answer here