Approaches to Work with JSP Tag Library in Java Web Application

There are 2 approaches to work with jsp tag library in our java web application:

Approach1: by using user defined taglib uri

Note: – our StrutsDemoApp1 is using this approach number (1) to work with Struts supplied html tag library.

Approach2: by using fixed taglib uri

Procedure to work with approach (1) based JSP tag libraries utilization:-

1)      Keep tag handler classes related jar files in WEB-INF/lib folder.

2)      Gather tld file of jsp tag library place in web application like WEB-INF folder.

3)      Configure jsp tag library with user defined tag lib uri in web.xml file.

4)      In the jsp programs of web applications specify user defined tag lib uri and start working with the jsp tags of tag library.

  • Every jsp tag library related tld file contains one fixed taglib uri you can collect from the <uri-tag> of that tld file.
  • Struts supplied jsp tag library related fixed taglib uri’s.

Html tag library –> http://Struts.apache.org/tags-html

Bean tag library –> http://Struts.apache.org/tags-bean

Logic tag library –> http://Struts.apache.org/tags-logic

Nested tag library –> http://Struts.apache.org/tags-neated

Tiles tag library –> http://Struts.apache.org/tags-tiles

Procedure to work with jsp tag library based on approach (2):-

1)      Keep jar files is WEB-INF/lib folder which represents tag handler classes & tld files of jsp tag library. (Like Struts-taglib-1.3.8.jar)

2)      Gather fixed tag lib uri form tld file and specify in jsp program then start working with tags of tag library. (The uri’s as shown above)

Register.jsp with uri of Struts supplied html tag library

   Register.jsp

      <%@ taglib uri = http://Struts.apache.org/tags-html prefix = “j” %>

            <j: from action = “register” method = “get” %>

                —–

                —–

         <j: submit value – “checkDetails”/>

       </j: form>

Note: – approach (2) is recommended to use. 

  • When source servlet uses rd.forward () is communicates with destination web resource program directly. The source & destination programs will use some request, response objects.
  • When source servlet program uses response.sendRedirect () it communicates with destination of web resource program by having one network round trip with browser window. So Source & destination programs will not use some request & response objects.

<action path – “/register” type =”app.RegisterAction” name = “rf”>

<forward name = “success” path =”/success.jsp” redirect = “true”/>

<forward name = “failure” path =”/failure.jsp” redirect = “false”/>

  • In the above code redirect = “true” means ActionServlet uses response.sendRedirect() method to transfer control to result page.(success.jsp) that means ActionServlet & result page, Action class & result page will not use same request & response object.
  • Redirect = “false” (default value) ActionServlet uses rd.forward () method to send the control to result page (failure.jsp).this indicates ActionServlet & result page, Action class & result page will use same request & response object.

Note: – ActionServlet & Action class uses same request & response objects irrespective of whether redirect = true/false.

Servlet Recap:-

   Attribute is a logical name that can hold a value; attributes are usefull to pass data from one web resource program to another web resource program.

Servlet Recap 

  • In the above diagram Srv 1, Srv2 & Srv3 are participating in a Servlet chaining so they use same req. & res. Object.
  • Request attribute created in Srv1 program is visible & accessible in Srv2 & Srv3 but not in Srv4.
  • Session attribute created in Srv1 by getting request from browser window bl is visible & accessible if remaining servlet programs only when they get request from same browser window bl.
  • Servlet context attribute created in Srv1 by getting request from browser window bl is visible & accessible if remaining servlet programs irresopnding of the browser window from which they are getting request.

Leave a Reply

Your email address will not be published. Required fields are marked *