June 20 2015

Jsp usebean example

Jsp usebean example

<jsp:useBean> action tag is used to instantiate or locate java bean instance and associate it with a jsp scripting variable.

Attributes

id – represents the name of java bean

scope – represents the scope of the java bean. Valid values are page, request, session and application. The default values is page.

class – takes the qualified class name to create a java bean if java bean is not available in specific scope

type – takes the qualified class name or interface name. A bean can implement many interfaces, the desired interface type can be specified in type attribute. Type attribute can be used with or without class or beanName attributes. If only type attribute is specified and no matching object is found, jsp container throws InstantiationException exception

beanName – takes the qualified class name. A jsp container uses java.bean.Beans class instantiate method to instantiate java bean

<jsp:setProperty>

Sets the value of a property by using setter method of java bean

Attributes

name – name of java bean (id attribute value of <jsp:userbean>)

property – takes the name of property to set

value – value that has to set to specified property

param – takes the request parameter name whose value has to be assigned to specified property

<jsp:getProperty>

retrieves value of the property using getter methods

Attributes

name – name of java bean (id attribute value of <jsp:userbean>)

property – property name whose values has to be retrieved

 

Jsp usebean example

Student.java


package com.answersz;

// Java usebean example
public class Student {

private int studId;
 private String name;
 private int age;

public int getStudId() {
 return studId;
 }

public void setStudId(int studId) {
 this.studId = studId;
 }

public String getName() {
 return name;
 }

public void setName(String name) {
 this.name = name;
 }

public int getAge() {
 return age;
 }

public void setAge(int age) {
 this.age = age;
 }

}

JspUseBeanExample.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Jsp usebean example</title>
</head>
<body>

<jsp:useBean id="studentbean" scope="session" class="com.answersz.Student"></jsp:useBean>

<jsp:setProperty name="studentbean" property="studId" value="1" />
 <jsp:setProperty name="studentbean" property="name" value="answersz" />
 <jsp:setProperty name="studentbean" property="age" value="20" />

 <jsp:getProperty name="studentbean" property="studId" />
 <jsp:getProperty name="studentbean" property="name" />

 <%=studentbean.getAge()%>

</body>
</html>

Output

http://localhost:8080/jsp-answersz/JspUseBeanExample.jsp

1 answersz 20


Copyright © 2014. All rights reserved.

Posted June 20, 2015 by answersz in category Jsp

Leave a Comment

Your email address will not be published. Required fields are marked *

*