You are on page 1of 2

How to Deploy Java beans in the Tomcat 6.

0+ demonstrated with an example:

0>> Stop the tomcat Server.

1>> For Servlets running under Tomcat 6.0:


In Tomcat 6.0 all the Servlets class files are placed under the folder
<TOMCAT_HOME>/webapps/examples/web-inf/classes/
and after that make changes in the deployment descriptor, found under the
same folder with the name "web.xml". The changes are
<servlet-mapping>
<servlet-name>Name of the Servlet Class</servlet-name>
<url-pattern>/servlet/Name of the servlet class</url-pattern>
</servlet-mapping>
Here "/servlet/" in the "url-pattern" is the prefix for the path of the
servlet class, it must be reflect in the accessing the servlet from a jsp
file as, http://localhost:8080/examples/servlet/<NameoftheServlet>

2>> Creating a bean class:


In the same way we can also access the Java Beans from a JSP page, it
is as show below
First create a Java Bean with the name MyBean[assumed as an example you
create your own bean] in the folder Beans[Folder is assumed as an
example you make your own]. The samp,e code for MyBean.java is

Source code:
package Beans; //Beans is the name of the package under which
the beans class file is placed

public class MyBean {


private String myName = ""; //Bean's property
public MyBean() {
myName = "Indian";
}
public void setMyName(String name) { //Bean Property setter method
myName = name;
}
public String getMyName() { //Bean property getter method
return myName;
}
}

Save the above code with name MyBean.java and compile the MyBean.java.

3>> Placing the bean class along with the package:


Now move/copy the folder "Beans" into the dir webapps/examples/web-
inf/classes/
That's it you have now a bean ready to access and operate.

4>> Accessing the bean from a JSP file:


Create a .jsp file that access the bean and uses the Bean's Getter and
Setter methods in order to access the Bean's private property myName.
Here the jsp file is named as MyBeanTest.jsp[The name is assumed as an
example you can create your own] and this file placed under the directory
webapps/examples/jsp/MyBeanTest.jsp
Source code:

<%@ page contentType="text/html; charset=ISO-8859-1" session="true" %>


<jsp:useBean scope="session" id="mybean" class="Beans.MyBean">
<jsp:setProperty name="mybean" property="myName" value="I am an
Indian" />
</jsp:useBean>
<%-- Method one to access a bean property from scriptlet --%>
<%
String hereMyName = mybean.getMyName();
%>
<html>
<head>
<title>MyBean Test Page</title>
</head>
<body bgcolor="#ffffff" text="green">
<div><h1>Hello </h1><%= hereMyName %>
<!-- Method 2 to access beans property using jsp action tag
<jsp:getProperty>
<jsp:getProperty name="mybean" property="myName" />
-->
</div>
</body>
</html>

Now by running athe above jsp file you can directly access the Bean you just
created and getting the bean property data using it's instance .

5>> Start the Tomcat Server and type the following URL,
http://localhost:8080/examples/jsp/MyBeanTest.jsp

output : Hello
Iam an Indian

By getting this ouput from the above files indicates you succeded in the
Bean usage under Tomcat 6.0+.

Here all we do is packaging the bean and accessing it from a jsp


file.
You must make the beans constructor and all it's methods as public if not
you get errors from tomcat. As well as you can define beans properties
as private.

Find more at
http://www.NaiduMCA.co.cc and
http://www.docstoc.com/profile/StudentPowerMCA
Access more and Learn more.
Thank you.

You might also like