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 /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 Name of the Servlet Class /servlet/Name of the servlet class 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/ 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/webinf/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: MyBean Test Page Hello --> 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.