22 June, 2012

JSTL a rich Web solution for JSP ! Kick your scriptlet over JSP.

By our traditional web development with J2EE is quite deprecated.Now-a-days there are ample of technology provided by different vendors like sun, oracle, micro soft , etc. Out of all JSTL(Java Standard Tag Library) a tech of sun has supreme power to optimize    and encapsulate the core functionality that common to many JSP applications. Instead of mixing tags from numerous vendors in your JSP applications, JSTL allows you to employ a single, standard set of tags.The sun has provides enormous benefits through JSTL.


  I am really disappointed; I am not using JSTL in my current project. Although I know it has many benefits over JSP scriptlet tags. I really want to kick out the scriptlet tags from my jsp. Scriptlets are difficult to maintain & execute by container also.It is a complete solution for web developers/front end developer , no need of prior knowledge of java/j2ee.It support full tag syntax, no need of jsp scriptlet tags.It has following major tag areas :-
      
         -- Core Tags
         -- Formatting Tags
         -- SQL Tags
         -- XML Tags
         -- Function Tags ( JSTL Functions)

There are many advantages of JSTL over JSP. 

         -- It is easy to use, ( avoid scriptlet tags)
         -- We can create our own custom tags
         -- These tags are standardize by sun.
         -- More functionality with easy iteration support tags
         -- XML supports
         -- It uses EL (Expression Language )  

You can get more about these from below given link from sun.

But, its quite simple to use JSTL with your jsp.I have posted here an easy example of JSTL below:-

Before go to the example, you need certain setups & resources like jstl.jar and  standard.jar.

1. You can Download the binary distribution from Apache Standard Taglib and unpack the compressed jar file.
2. Extract those files , and copy all extracted files ( .tld and other files) into WEB-INF\lib .

3. set some necessary tag-lib inside web.xml & enjoy the flavor of JSTL.

Simple Example-


web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
      <taglib>
          <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
          <taglib-location>/WEB-INF/lib/c.tld</taglib-location>
      </taglib>
       </jsp-config>
</web-app>

jsp file


<%@ taglib uri="/WEB-INF/lib/c.tld"  prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
 
  <body>
  <table>
    <c:forEach var="i"  begin="1" end="10" step="1">
    <tr>
        <c:forEach var="k" begin="1" end="5" step="1">
            <td><c:out value="${k}"></c:out></td>
        </c:forEach>
    </tr>
    </c:forEach>
    </table>
          </body>
</html>

 
Now start the tomcat and run from browser. The out put of the program will be like below :-


I have only used core tag in this example. But there are so many tags including other tag sections discuss above. Also you can develop you own .tld file and own custom tags. 


Read all about JSTL from sun.

By Manoj.

No comments:

Post a Comment