JSP-JavaBean
Jsp to Java Bean communication:-
-->Java bean is helper class in java project development having ability to carry huge amounts of data from one component to other component in the form of single object
-->The form data coming jsp/servlet component will be passed to service/DAO class in the form of single java bean class object
-->For servlet to java bean communication, we need to create object of JavaBean class directly in servlet component
-->For jsp to java bean communication we need to use the following tags
a.)<jsp:useBean>
b.)<jsp:setProperty>
c.)<jsp:getProperty>
a.)<jsp:useBean>
-->Useful to create or locate java bean class object from the specified scope, if created it allows to keep java bean class object in the specified scope, if located it gets java bean class object from the specified scope
Syn:
- <jsp:useBean attributes/>
Attributes:
id=To specify bean id(reference variable name that holds bean class object)
class=The fully qualified java bean class name
scope=To specify bean object scope(page/request/session/application)
type=To specify the super class name of bean class as the reference type
E.g:
- public class StudentBean{
- private int sno;
- private String sname,result;
- //setters & getters
- .....
- }
- <jsp:useBean id="st" class="<pkg>.StudentBean" scope="session"/>
-->Creates StudentBean class object having reference variable name "st" and keeps the object in session scope
JES class code
- StudentBean st=null;
- Synchronized(session){
- st=pageContext.getAttribute("st", pageContext.SESSION_SCOPE);
- if(st==null){
- st=new StudentBean();
- pageContext.setAttribute("st", pageContext.SESSION_SCOPE);
- }//if
- }//method
E.g(2):
- <jsp:useBean id="st"
- class="<pkg>.StudentBean"
- type="<pkg>.PersonBean"
- scope="session"/>
Note: PersonBean must be super class of StudentBean
(ref type) st=new StudentBean()(obj type);
Note: If "type" attribute is not specified in <jsp:useBean> tag then bean class acts as both reference type and object type
<jsp:setProperty>
-->It is given to call setter methods on bean class objects and to write data to bean properties
<jsp:setProperty attributes>
Attributes
name= Bean id given in <jsp:useBean> tag
property= Bean property name (or) xxx part of setXxx(-,-) method
value= The value to be assigned
param= Request param value whose value to be assigned to bean property
Note: Use either "value" or "param" attribute
E.g(1)
- <jsp:setProperty name="st" property="sno" value="101"/>
- -->Calls st.setSno(101) to assign 101 to bean property
E.g(2)
- <jsp:setProperty name="st" property="sname" param="stname"/>
- -->Reads "stname" request param value to assign to "sname" property by calling st.setSname(-) method
- Code: st.setSname(request.getParameter("stname"));
<jsp:getProperty>
-->Reads and displays the given bean property value
Syn:
- <jsp:getProperty attributes>
Attributes
name= Bean id
property= Property name or xxx part of getXxx(-) methods
E.g:
- <jsp:getProperty name="st" property="sno"/>
- -->Reads and displays the "sno" bean property value
JspApp13-useBeanPOC
|-->java resources
|-->org.bean
|-->StudentBean.java
|-->webcontent
|-->web.xml
-->Give 1st request to SetValues.jsp and gives 2nd request to GetValues.jsp
-->To set form data(request param values) as bean property values we need to use param attribute of <jsp:setProperty> tags
- <jsp:setProperty name="st" property="sno" param="stno"/>
- <jsp:setProperty name="st" property="sname" param="stname"/>
- <jsp:setProperty name="st" property="result(bean propertyname)" param="stresult(req param name)"/>
http://localhost:9000/JspApp13-useBean/setValues.jsp?stno=101&stname=teja&sresult=pass
-->If bean property names are matching with request param names then we can specify property="*" in <jsp:setProperty> tag to assign request param values to bean properties
- <jsp:setProperty name="st" property="*"/>
request url's is:
http://localhost:9000/JspApp13-useBean/SetValues.jsp?sno=101&sname=tej&sresult=pass
UseCase by using useBean tag:-
-->The page slip method of service class uses DTO object to gather inputs and also uses to set generated outputs
JspApp14-UseBeanUseCase
|-->java resources
|-->org.service
|-->SalaryService.java
|-->org.dto
|-->EmployeeDTO.java
|-->webcontent
|-->form.html
|-->process.jsp
|-->WEB-INF
|-->web.xml
-->In the above application we use single DTO(java bean) to carry inputs to service class from jsp and also to carry outputs to jsp from service class
Thanks...
ReplyDeleteAwesome blog. Your articles really impressed for me, because of all information so nice and unique. Java Training in Chennai
ReplyDelete