15) Explain JSP unified Expressin Language (EL). (May-13, Jun-12) EL is a language that allws JSP prgrammers t fetch applicatin data stred in JavaBeans cmpnent. The fllwing methds are used t call the java cde frm a JSP page: 1. Placing the entire Java cde in a JSP page 2. Defining separate helper classes that encapsulate the entire Java cde and calling these helper classes frm a JSP page 3. Using JavaBeans and JSP page actin tags 4. Using JSP EL 5. Create tag handler classes t embed the Java cde The incrpratin f EL t the JSP has helped reduce the use f scriptles in JSP page. EL expressins prvide shrt hand ntatins t retrieve, present, and manipulate Web applicatin data. EL expressins are enclsed between the ${ and} characters. The mst general example f an EL expressin is ${bject.data} Where, bject can be any Java bject representing different scpe, such as request,sessin. EL expressings can be categrized int the fllwing types: 1. Immediate and deferred expressins 2. Value expressins 3. Methd expressins 1) Immediate and deferred expressins Tw cnstructs are used t represent EL expressins, ${ expr } used fr expressins that need t be evaluated immediately, and #{ expr } used fr expressins that need t be evaluated at later time. An expressin that uses ${ expr } is called immediate expressin and The expressin that uses #{ expr } is called deferred expressin. 2) Value Expressins Value epressins are used t refer t bjects such as JavaBeans, Cllectins, enumeratins and implicit bjects and their prperties. An bject is referred t by using the value expressin cntaining the name f the bject Suppse the ${ emplyee } expressin is used in a JSP page, where emplyee refers t the name f a Which searches fr the emplyee JavaBeab in the request, sessin and applicatin scpes If the emplyee JavaBean des nt exist, a null value is returned. Syntax f value expressin t access prperties and elements f a cllectin in java uses tw peratrs, (. ) and ( [] ). Fr example, The name prperty f the emplyee JavaBean can be referred as ${emplyee.name} r ${ emplyee[ name ] } expressin. T access a specific element f a cllectin, such as list r an array inside a JavaBean we can use ${ emplyee.name[0] }, ${ emplyee.name[1] }, etc The value expressins are f tw types: rvalue rvalue expressin can nly read data, and nt write data. lvalue lvalue expressin can read as well as write data. 3) Methd Expressins Methd expressins are used t call public methds, which returns a value r bject. Such expressins are usually deferred expressins. Arjun V. Bala Page 25
Tags usually use methd expressins t call functins that perfrm peratins such as validating a UI cmpnent r handling the events generated n a UI cmpnent. The fllwing cde snippet shws the use f methd expressins in JSF page: <h:frm> <h:inputtext id= email value= #{emplyee.email} validatr= #{emplyee.validateemail} /> <h:cmmandbuttn id= submit actin= #{custmer.submit} /> </h:frm> The varius elements shwn in the preceding cde snippet can be briefly described as fllws: The inputtext tag : shws the UIInput cmpnent in the frm f a text field n a webpage The validatr attribute : calls the validateemail methd f the emplyee JavaBean The actin attribute f the cmmandbuttn tag : calls the submit methd, which carries ut prcessing after submitting the webpage The validateemail methd : Refers t the methd that is called during the validatin phase f the JSP life cycle The submit methd : Refers t the methd that is called during the invke applicatin phase f the JSP life cycle 16) Enlist and explain the use f actin tags in JSP. (May-13,Jan-13) Actin tags are specific t a JSP page. When a JSP cntainer encunters an actin tag while translating a JSP page int a servlet, it generates a Java cde crrespnding t the task t be perfrmed by the actin tag. The fllwing are sme imprtant actin tags available in JSP page: 1. <jsp:include> The <jsp:include> actin tag facilitates Java prgrammers in including a static r dynamic resurce, such as an HTML r JSP page in the current JSP page. If the resurce t be included is static then its cntent is directly included. If the resurce is dynamic then first it is prcessed and result is included in page. Example <jsp:include page= test.jsp /> 2. <jsp:frward> The <jsp:frward> tag frwards a JSP request t anther resurce which can be either static r dynamic. If the request is frwarded t a dynamic resurce, a <jsp:param> tag can be used t pass a name and value f a parameter t a resurce. <jsp:frward page= /header.html /> 3. <jsp:param> The <jsp:param> tag allws Java prgrammers t pass a name and value f a parameter t a dynamic resurce, while including it in a JSP page r frwarding a request t anther JSP page. <jsp:param name= uname value= arjunbala /> Arjun V. Bala Page 26
4. <jsp:usebean> T separate te business lgic frm the presentatin lgic, it is ften a gd idea t encapsulate the business lgin in Java bject, and then instantiate and use this Java bject within a JSP page, <jsp:usebean> tag helps in such task. The <jsp:usebean> actin tag has certain attributes that add extra characteristics t it, sme attributes specific t <jsp:usebean> are: Id : represents the name assigned t a JavaBean, which is later used as a variable t access the JavaBean Class : takes the qualified class name t create a JavaBean instance if the JavaBean instance is nt fund in the given scpe. beanname : takes the qualified class name r expressin f a JavaBean t create a Class & beanname can nt be used tgether. We can nt use expressins t create instance f JavaBean frm class attribute, fr that we have t use beanname attribute. Scpe : represents the scpe in which a JavaBean instance has t be created. Page scpe : indicates that a JavaBean can be used where <jsp:usebean> actin tag is used. Request scpe : indicates that a JavaBean can be used frm any JSP page, which prcesses the same request until a respnse is sent t a client by the JSP page. Sessin scpe : indicates that a JavaBean can be used frm any JSP page invked in the same sessin as the JSP page that created the Applicatin scpe : indicates that a JavaBean can be used frm any JSP page in the same applicatin as the JSP page that created the Example : <jsp:usebean id= mybean class= MyBeanClass scpe= sessin >. </jsp:usebean> 5. <jsp:setprperty> The <jsp:setprperty> actin tag sets the value f a prperty by using the setter methd f a Befre using the <jsp:setprperty> actin tag, the JavaBean must be instantiated. Example: <jsp:setprperty name= beanname prperty= uname value= arjunbala /> 6. <jsp:getprperty> The <jsp:setprperty> actin tag retrives the value f a prperty by using the getter methd f a JavaBean and writes the value f the current JspWriter. <jsp:getprperty name= mybean prperty= uname /> 7. <jsp:plugin> The <jsp:plugin> actin tag prvides supprt fr including a Java applet r JavaBean in a client Web brwser, by using built-in r dwnladed Java plug-in. The <jsp:plugin> actin tag perfrm the fllwing peratins: Specify whether the cmpnent added in the <bject> tag is a JavaBean r an applet. Lcate the cde that needs t be run. Arjun V. Bala Page 27
Psitin an bject in the brwser windw. Specify a URL frm which the plug-in sftware is t be dwnladed. Pass parameter names and values t an bject <jsp:plugin attributes>. </jsp:plugin> 8. <jsp:params> The <jsp:params> actin tag sends the parameters that yu want t pass t an applet r <jsp:params> <!- ne r mre jsp:param tags --> </jsp:params> 9. <jsp:fallback> The <jsp:fallback> actin tag is allws yu t specify a text message that is displayed if the required plug-in cannt run. This tag must be used as a child tag with <jsp:plugin> actin tag. Example: <jsp:fallback> Text message that has t be displayed if the plugin cannt be started </jsp:fallback> 10. <jsp:attribute> The <jsp:attribute> actin tag is used t specify the value f a standard r custm actin attribute. <jsp:setprperty name= mybean > <jsp:attribute name= prperty >uname</jsp:attribute> <jsp:attribute name= value >Arjun Bala</jsp:attribute> </jsp:setprperty> 11. <jsp:bdy> The <jsp:bdy> actin tag is used t specify the cntent (r bdy) f a standard r custm actin tag. <jsp:userbean id= mybean >.. <jsp:bdy>. </jsp:bdy> </jsp:mybean> 12. <jsp:element> The <jsp:element> actin tag is used t dynamically define the value f the tag f an XML element. <jsp:element name= mytag > <jsp:attribute name= myatt >My Value </jsp:attribute> </jsp:element> 13. <jsp:text> A <jsp:text> tag is used t enclse template data in an XML tag. The cntent f the <jsp:text> bdy is passed t the implicit bject ut. <jsp:text> Hell wrld frm the actin tag </jsp:text> Arjun V. Bala Page 28
17) Explain JSP standard tag library (JSTL) with examples. (Jun-12,Nv-11) Initially, web designers used scriplets in JSP pages t generate dynamic cntent. This resulted in redability issues and als made it difficult t maintain the JSP page. Custm tags were intrduced t vercme the prblems faced in using scriplets, they had sme limitatins t. Web designers had t spend a lt f time in cding, packaging, and testing these tags befre using them. This meant that web designers were ften left with little time t cncentrate n the designing f web pages. The intrductin f JSTL has helped web designers vercme the shrtcmings f custm tags, by encapsulating the cmmn functinalities included the use f tag libraries, such as cre, SQL, and XML. The main features f JSTL are: Prvides supprt fr cnditinal prcessing and Unifrm Resurce Lcatr (URL)- related actins. Prvides the XML tag library, which helps yu t manipulate XML dcuments. Enables Web applicatins t be accessed glbally by prviding the internatinalizatin tag library. Enables interactin with relatinal databases by using varius SQL cmmands. Prvides series f functins t perfrm manipulatins. Tag Libraries in JSTL: Tag Library Functin URI prefix Cre tag library Variable supprt http://java.sun.cm/jsp/jstl/cre c Flw Cntrl Iteratr URL management Miscellaneus XML tag library Flw cntrl http://java.sun.cm/jsp/jstl/xml x Transfrmatin Lcale Internatinalizatin Message frmatting http://java.sun.cm/jsp/jstl/fmt fmt tag library Number and date frmatting SQL tag library Database manipulatin http://java.sun.cm/jsp/jstl/sql sql Functins Library Cllectin length String manipulatin http://java.sun.cm/jsp/jstl/functins fn Arjun V. Bala Page 29
Example f Cre tag library: <%@ taglib uri="http://java.sun.cm/jsp/jstl/cre" prefix="c" %> <html> <head> <title><c:chse> Tag Example</title> </head> <bdy> <c:set var="salary" scpe="sessin" value="${2000*2}"/> <p>yur salary is : <c:ut value="${salary}"/></p> <c:chse> <c:when test="${salary <= 0}"> Salary is very lw t survive. </c:when> <c:when test="${salary > 1000}"> Salary is very gd. </c:when> <c:therwise> N cmment... </c:therwise> </c:chse> </bdy> </html> Arjun V. Bala Page 30