JSP (Java Server Pages) and adding JSTL to your Web Application

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

This page is about installing JSTL - the Java Standard Tag Library - in JSP Pages. JSTL provides useful tags for

  • If..then conditions
  • Looping
  • Outputting values from session to screen
  • Page level variable maniupulation
  • Localisation


Contents

See Also

  • Spring MVC - gives details on how to install and use JSTL with Spring


Useful JSP Code

  • Using the context to ensure resource paths are always correct
<% String context = request.getContextPath();%>
 <script language="javascript" src="<%=context%>/javascript/someJavascriptFile.js"></script>

Installing JSTL and adding to your web application

  1. Ensure JSTL Libs are in your WEB-INF/lib or your deployed war file
  2. Add the following to your web.xml
<taglib>
    <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
    <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
    <taglib-location>/WEB-INF/sql.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
    <taglib-location>/WEB-INF/x.tld</taglib-location>
  </taglib>
  1. Import and use on your page
<!-- Standard JSTL taglib -->
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<c:out value="${someBeanInSession.nameOfVarOnMyBean}"/>
 
<!-- Note that the ${someVarInSession} syntax can be used outside of the tags, as long as it is within quotes.  -->

External Links

Personal tools