Wednesday, April 13, 2011

Chapter 39: Creating JavaBeans

In the previous chapter, we saw what JavaBeans are and the fact that they can be used in JSPs to enhance the functionality of the application. In this chapter we are going to take a look at how to create JavaBeans.

So, lets get started!!!

Creating a JavaBean:

A JavaBean is nothing but a regular Java class but with a slight difference. It has a bunch of variables declared and then a pair of corresponding get/set methods for each of these variables. And the other difference is the fact that, these beans have a default no-arg constructor only. Value constructors are not present in a JavaBean.

Example Java Bean:

Let us say we want a bean that can be used to validate a users profile in a website – a login id and password combination. This is how the bean would look like:
package test.scwcd;

public class LoginBean
{
//The No-Arg constructor
public LoginBean()
{
}

//Variable Declaration
private String username = "";
private String password = "";

//Get Methods

public String getUsername()
{
return(username);
}
public String getPassword()
{
return(password);
}

// Set Methods

public void setUsername(String value)
{
username = value;
}
public void setPassword(String value)
{
password = value;
}

//Other methods that contain business logic
// Here – a method to validate a users id/pwd combination
public boolean confirmLogin()
{
boolean valid = true;
if (username.length() == 0 ||
password.length() == 0)
{
valid = false;
}

return(valid);
}
}


Now that we have seen how a bean looks, lets take a look at a corresponding JSP page that is going to use this bean.

JSP Code:

< % @ page session="false" % >
< jsp : useBean id="security" class=" test.scwcd.LoginBean" / >
< jsp : setProperty name="security" property="*" / >

< % if (security.confirmLogin()) { % >
< jsp : forward page="welcome.jsp" / >
< % } % >

< html >
< head >
< title > JavaBean Example < / title >
< / head >
< body bgcolor = " #FFFFFF " >
< p / >
< h1 > JavaBean Example< / h1 >
< form method="POST" action="login.jsp" >
< table >
< tr >
< td >
User name
< / td >
< td >
< input name="username" size=15 value="<%=security.getName()%>" >
< / td >
< / tr >
< tr >
< td >
Password
< / td >
< td >
< input type=password name="password" size=15 >
< / td >
< / tr >
< tr >
< td colspan="2" align="center" >
< input type="submit" value=" Login " >
< / td >
< / tr >
< / table >
< / form >
< / center >
< / body >
< / html >

As you can see above, we have used the jsp:useBean tag to include a Bean class inside a JSP file. A point to note here is that, when you use jsp:useBean and specify the class, it must be in the correct class path.

This can be confusing – the setting of classpath and placing the beans in the appropriate directories.

In a Tomcat server running on Windows, the servlets directory has the default path of CATALINA_HOME\webapps\examples\WEB-INF\classes\.

I need to place this bean class in the folder CATALINA_HOME\webapps\examples\WEB-INF\classes\test\scwcd\LoginBean.class

Notice that the package here is test.scwcd and the class file goes into a sub-folder \test\scwcd\ inside the classes directory.

Another thing is that – a bean class can always be placed in either the WEB-INF/classes/ directory or in the WEB-INF/lib directory (If the bean exists inside a JAR file)

Previous Chapter: Chapter 38 - Introduction to JavaBeans

Next Chapter: Chapter 40 - Using jsp:useBean

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers