0% found this document useful (0 votes)
41 views10 pages

JSP Beans

Uploaded by

Ashok Nutalapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
41 views10 pages

JSP Beans

Uploaded by

Ashok Nutalapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Using Beans in JSP Pages Using Beans in JSP Pages * A Java Bean is a specially constructed Java class written in the Java. * Java Beans are java classes that have properties, which can be read via a get method or changed via a set method. * A Java Bean should not have any public variables. All the variables should be accessed using the get/set methods. JavaBeans Properties * A JavaBean property is a named attribute that can be accessed by the object. The attribute can be of any Java data type, including the classes that you define. * JavaBean properties are accessed through two methods in the JavaBean's implementation class. Using Beans in JSP Pages getPropertyName() * For example, if property name is firstName, your method name would be getFirstName() to read that property. setPropertyName() * For example, if property name is firstName, your method name would be setFirstName() to write that property. Accessing JavaBeans * Following JSP action elements are required to use Java bean in a JSP file. — Load Java bean inside a JSP : * We need to use tag to load a bean into JSP. * The basic syntax is given below: Getting the Properties of the Bean: « After the bean gets loaded into the page, the properties can be accessed by using the action element. * The basic syntax is given below: Setting the Properties of the Bean : * To modify (or) assign value to any variable of the bean object the action element is used . * The basic syntax is as below Example: “Employee.java” Step 1: Create Java Bean package college; public class Employee { private String name = "Madhu"; private String department = "CSE"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } } Example: “jbeandemo.jsp” Step 2: Create JSP Java Bean Demo

Old Employee details

Name: Durga" /> mployee" property="name" />

New Employee details

Name:
Department : Step 3: Create Folder Structure webapps JSP Files ‘WEB-INF —— classes [eit Class Files web.xml file —} lib Static Resources(HTML,css,images..) Execution Procedure: 1.We need to compile Employee.java file. 2.After successful compilation we get package along with class file. college | > Employee. class 3.We need to copy Employee. class along with college package into classes folder which was in WEB-INF folder. 4.After copying, We need to execute JSP file(jbeandemo.jsp) using Tomcat server. i.e., open any browser give following as URL localhost:8080/foldername/jbeandemo.jsp Output: ting the Values of JavaBean x < CS _ @ tecathost:8080/jspex/beandemojsp Old Employee details Name:Madhu Department :CSE New Employee details Name:Durga Department :CSE Example: “Calculator.java” * Create Java Bean package mycalc; public class Calculator{ Ba loca x public int cube(int n) € 3 © O bocalhost8080,; { cube of Sis 125 return n*n*n; } } * Create JSP file “calc.jsp” <% int m=obj.cube(5); out.print("cube of 5 is "+m); %>

You might also like