JSP Action Elements Lec - 37
JSP Journey directive elements ………………………. scripting elements JSP comments………………………………. declarations ………………………………… expressions …………………………..…….. scriptlets……………………………….......... action elements special JSP tags ………………………….…. implicit objects
JSP Action Elements
JSP Action Elements Format Expressed using XML syntax Opening tag <jsp:actionElement attribute=“value” … > Body body Closing tag </jsp:actionElement > Empty tags without body <jsp:actionElement attribute=“value” … />
JSP Action Elements cont. Purpose To work with JavaBeans To include pages at request time To forward request to another resource etc.
Some JSP Action Elements To work with JavaBeans <jsp:useBean /> <jsp:setProperty /> <jsp:getProperty /> To include resources at request time <jsp:include /> To forward request to anther JSP or Servlet <jsp:forward /> To work with applets <jsp:plugin /> And many more….
Working with JavaBeans using JSP Action Elements
Working with JavaBeans using JSP action elements JSP action elements also helps to work with JavaBeans <jsp:useBean /> Makes a JavaBeans object available in a page <jsp:setProperty /> Sets a JavaBeans property value <jsp:getProperty /> Gets a JavaBeans property value
JSP useBean Action Element jsp:useBean is being equivalent to building an object in scriptlet <% MyBean m = new MyBean(); %> OR <jsp:useBean id = “m” scope = “page” class=“vu.MyBean ”/>
JSP setProperty Action Element jsp:setProperty is being equivalent to following code of scriptlet <% m.setName(“ali”); %> OR <jsp:setProperty id = “m” property = “name” value=“ali” />
JSP getProperty Action Element jsp:getProperty is being equivalent to following code of scriptlet <% String name = m.getName(); out.println(name); %> OR <jsp:getProperty id = “m” property= “name” />
Sharing Beans & Object Scopes
Understanding page Scope first.jsp second .jsp third.jsp request 1 request 1 or request 2 create Values not available MyBean m = new MyBean(); MyBean m m.setName(“ali”); [name = ali] Page Context
Understanding request Scope first.jsp second .jsp third.jsp fourth.jsp request 1 request 1 request 2 create values not available values available MyBean m [name = ali] ServletRequest
Understanding session Scope first.jsp second .jsp third.jsp fourth.jsp request 1 request 1 request 2 create values available values available MyBean m [name = ali] HttpSession
Understanding application Scope first.jsp second .jsp third.jsp fourth.jsp request 1 request 1 request 2 create values available values available MyBean m [name = ali] ServletContext
Different Object Scopes Most visible page Objects may be accessed only within pages where they are created Least visible
Different Object Scopes Most visible Only within pages processing the request in which they are created request page Objects may be accessed only within pages where they are created Least visible
Different Object Scopes Most visible Only from pages belonging to same session as the one in which they were created session Only within pages processing the request in which they are created request page Objects may be accessed only within pages where they are created Least visible
Different Object Scopes Within all pages belonging to same application Most visible application Only from pages belonging to same session as the one in which they were created session Only within pages processing the request in which they are created request page Objects may be accessed only within pages where they are created Least visible
Object Scopes session, request & page
Object Scopes session vs. application
More JSP Action Elements
JSP include Action Element cont. jsp:include is being equivalent to following code of scriptlet <% RequestDispatcher rd = request.getRequestDispatcher(“one.jsp”); rd.include(request, response); %> OR <jsp:include page = “one.jsp” flush= “true” />
JSP forward Action Element To forward request to another resource Format <jsp:forward page=“one.jsp" /> Works similar to following code of scriptlet <% RequestDispatcher rd = request.getRequestDispatcher(“one.jsp”); rd.forward(request, response); %> Notes -You are not permitted to specify false for the flush attribute (until JSP 1.2) -Don’t forget that trailing slash - XML syntax!