JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Using JavaBeans Components in JSP Documents



  • JavaBeans components are Java classes that can be easily reused and composed together into applications.
  • Any Java class that follows certain design conventions can be a JavaBeans component.
  • JavaServer Pages technology directly supports using JavaBeans components with JSP language elements.
  • You can easily create and initialize beans and get and set the values of their properties.

JavaBeans Component Design Conventions:

  • JavaBeans component design conventions govern the properties of the class and govern the public methods that give access to the properties.

JavaBeans Component property:

  • A JavaBeans component property can be:

  • Read/write, read-only, or write-only.
  • Simple, which means it contains a single value, or indexed, which means it represents an array of values..

  • For each readable property, the bean must have a method of the form:

propertyClass getProperty() { ... }

  • For each writable property, the bean must have a method of the form:

setProperty(PropertyClass pc) { ... }

Syntax:
  • One of the ways to set JavaBeans component properties in a JSP page is using a scriptlet.
<% beanName.setPropName(value); %>

  • Retrieving JavaBeans Component Properties:
<% Object o = beanName.getPropName(); %>

Example:
<% bookDB.setBookId(bookId); %>
<% int num = cart.getNoOfItems(); %>



public class Student{
String id = "";
String name = "";
public void setId(String id) {
this.id = id;
}
public String getId() { return id; }
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}



  
<%@page import="com.cts.Student" %>
<html>
<head>
<title>Acessing Bean Class Using Scriplet</title>
</head>
<body>
<% Student student=new Student();
student.setName("John");
student.setId("22"); %>
<h1>The Name and Id of the Students are<br></h1>
<h2>Name :
<% out.println(student.getName());
%><br>
Id :
<% out.println(student.getId()); %>
</h2>
</body>
</html>




Java Beans with JSP Actions

Java Beans and JSP

JavaServer Pages technology directly supports using JavaBeans components with JSP language elements.

You can easily create and initialize beans and set the values of their properties.

Creating and Using a JavaBeans Component:

JSP page will use a JavaBeans component, using either one of the following formats:

<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope"/>

or

<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope"> <jsp:setProperty .../> </jsp:useBean>.

Setting JavaBeans Component Properties:

Syntax:
<jsp:setProperty name=" beanName " property=" propName "/>
<jsp:setProperty name="bookDB" property="bookId"/>

 
Retrieving JavaBeans Component Properties:

Syntax:
<jsp:getProperty name="beanName“ property="propName"/>
<jsp:getProperty name="currency" property="format"/>



Example:
<jsp:setProperty name="bookDB" property="bookId"/>
<jsp:getProperty name="currency" property="format"/>

CODE:-  

<html>
<head>
<title>Acessing Bean Class Using JSP Action</title>
</head>
<body>
<jsp:useBean id="student" class="com.cts.Student"/>
<jsp:setProperty name="student" property="name" value="John"/>
<jsp:setProperty name="student" property="id" value="123456"/>
<h1>The Name and Id of the Students are<br></h1>
<h2>Name :
<jsp:getProperty name="student" property="name"/><br>
Id :<jsp:getProperty name="student" property="id"/></h2>
</body>
</html>

package com.cts;
public class Student{
String id = "";
String name = "";
public void setId(String id) {
this.id = id;
}
public String getId() { return id; }
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

 
How It Works:

  • The bean has to be compiled and the class file has to be present in the classpath.
  • Start the Tomcat server. Access the Beanaction.jsp page from the browser.
  • The JSP actions aids users develop scripless pages, which is easy in terms of maintaining and is easily readable.