Skip to main content

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:
  1. <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:
  1. public class StudentBean{
  2.  private int sno;
  3.  private String sname,result;
  4.  //setters & getters
  5.  .....
  6. }
  1. <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
  1. StudentBean st=null;
  2. Synchronized(session){
  3. st=pageContext.getAttribute("st", pageContext.SESSION_SCOPE);
  4. if(st==null){
  5.   st=new StudentBean();
  6.   pageContext.setAttribute("st", pageContext.SESSION_SCOPE);
  7. }//if
  8. }//method
E.g(2):
  1. <jsp:useBean id="st"
  2.  class="<pkg>.StudentBean"
  3.  type="<pkg>.PersonBean"
  4.  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)
  1. <jsp:setProperty name="st" property="sno" value="101"/>
  2. -->Calls st.setSno(101) to assign 101 to bean property
E.g(2)
  1. <jsp:setProperty name="st" property="sname" param="stname"/>
  2. -->Reads "stname" request param value to assign to "sname" property by calling st.setSname(-) method
  3. Code: st.setSname(request.getParameter("stname"));
<jsp:getProperty>
-->Reads and displays the given bean property value
Syn:
  1. <jsp:getProperty attributes>
Attributes
name= Bean id
property= Property name or xxx part of getXxx(-) methods
E.g:
  1. <jsp:getProperty name="st" property="sno"/>
  2. -->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
  1. <jsp:setProperty name="st" property="sno" param="stno"/>
  2. <jsp:setProperty name="st" property="sname" param="stname"/>
  3. <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
  1. <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

Comments

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

JSP Comments

Comments in JSP:- -->Compiler /Interpreter doesn't take commented code for compilation /Interpretation, so the commented code doesn't participate in execution JSP supports 3 types of comments:- a.)HTML comments /Template text comments/ Output comments Syn :- <!---text---> -->Recognized by html interpreter of browser b.)JSP comments /Hidden comments Syn :- <%--text--%> -->JSP page compiler recognize these comments c.)Java comments /Scripting comments Syn :- //-->For single line /*-- -----*/-->For multiple line -->Java compiler(java) recognize these comments -->In Eclipse IDE JES class for first.jsp comes in our(workspace)folder/.metadata/.plugins/.org eclipse Comments (1)JSP comment<%--%> (2)Java comment(// or /*--*/) (3)HTML comments(<!---> JES Source Code (1)No (2)Yes (3)Yes In JES compiled code (1)No (2)No (3)Yes In the code going to browser (1)No (2)No (3)Yes Output (1)No (2)No (3)No -->Jsp comments are not visible in any phase...

Scripting Tags

JSP Tags/ Elements (1)Scriptlet     Standard syn:-         <%.........%>     xml syn:-        <jsp:scriptlet>...</jsp:scriptlet> Note: All scripting tags allows us to place script code(java code) 1.)The code placed in scriptlet go to _jspService(-,-) of JES class 2.)In a jsp page we can have zero or more scriptlets 3.)We place request processing logics in scriptlets 4.)Variables declared in scriptlets becomes the local variables in _jspService(-,-) of JES class in first.jsp In first.jsp: <% int a=10;  out.println("square:"+(a*a));%> In first_jsp.java(JES): public class first_jsp extends..{     public void _jspService(-,-){        int t=30;        out.println(“Square:”+(t*t));     }  } The code placed in scriptlet can use implicit objects of jsp because implicit objects and the code placed scriptlet goes to _jspService(-...

Project Architecture's

1.) Functional flow/ Architecture a.) Only for 6+/7+years b.) Explain the flow of process/business 2.) Technical Flow/Architecture a.) Upto 5+ years b.) Explain technologies /components that are involved in the project Servlet & JSP Project Architecture:- 1.)What is need of Business Deligate to convert VO class object to DTO class object ? a.) If Business Deligate is not there servlet component directly prepares DTO class object by receiving and translating form data and passes to service class, if service class excepts certain input in other format like numeric employee number as string employee number then we need to modify servlet component code i.e For the changes that happened in business tier service component we have to modify servlet component of presentation tier b.) If Business Deligate is taken then it gets original form data as VO class object having string inputs and converts VO class object to DTO class object to pass to service class c.) If service class excepts ...