JSP Action Elements Lec - 37 JSP Journey – directive elements ………………………. – scripting elements implicit objects • • • • JSP comments………………………………. declarations ………………………………… expressions …………………………..…….. scriptlets……………………………….......... – action elements • special JSP tags ………………………….…. JSP Action Elements JSP Action Elements • Format – Expressed using XML syntax • Opening tag • Body <jsp:actionElement attribute=“value” … > 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 request 1 second .jsp create MyBean m = new MyBean(); m.setName(“ali”); MyBean m [name = ali] Page Context request 1 or request 2 third.jsp Understanding request Scope first.jsp request 1 second .jsp create MyBean m [name = ali] ServletRequest request 1 third.jsp request 2 fourth.jsp Understanding session Scope first.jsp request 1 second .jsp create MyBean m [name = ali] HttpSession request 1 third.jsp request 2 fourth.jsp Understanding application Scope first.jsp request 1 second .jsp create MyBean m [name = ali] ServletContext request 1 third.jsp request 2 fourth.jsp Different Object Scopes Most visible Least visible page Objects may be accessed only within pages where they are created Different Object Scopes Most visible request Least visible page Only within pages processing the request in which they are created Objects may be accessed only within pages where they are created Different Object Scopes Most visible Least visible session Only from pages belonging to same session as the one in which they were created request Only within pages processing the request in which they are created page Objects may be accessed only within pages where they are created Different Object Scopes Most visible Least visible application Within all pages belonging to same application session Only from pages belonging to same session as the one in which they were created request Only within pages processing the request in which they are created page Objects may be accessed only within pages where they are created 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); %>
© Copyright 2026 Paperzz