JSP Core JSTL

JSP Core JSTL

  • The JSP Standard Tag Library (JSTL) adds essential features to JSP that enable JSP programming without the need for embedded Java code.
  • It is a collection of standard JSP tags that perform several common tasks.
  • This frees us from having to develop custom tags for these tasks, or from using a mix of tags from several organizations to do our work.
  • The core JSTL contains tags that deal with flow control in JSP pages, iterating over collections, evaluating expressions, and importing resources.
  • Before using the core JSTL, the following directive needs to be added to the JSP page:
  • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  • The equivalent XML syntax used in tag files for this directive is:
  • <jsp:directive.taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" />

Here is a summary of all core JSTL tags

  • Tag Description
    catch This tag is used to catch any java.lang.Throwable exception in the body of the tag.
    choose This tag enables you to provide handling for a set of mutually exclusive conditions, instead of just one.
    if This tag evaluates a test condition. If it is true, it processes its body else the body is ignored.
    import Imports a resource specified by a URL and exposes it to the page, variable, or reader.
    forEach This tag is used for iteration over collections.
    forTokens This tag is used for iterating over tokens separated by a delimiter.
    out This tag is used to output the result of an expression.
    otherwise This action may appear only once within a choose action to provide handling of remaining alternatives.
    param This tag used along with the import, redirect, or url tags to add a parameter to a URL.
    redirect This tag redirects the user to a new URL.
    remove This tag removes a variable from a scope.
    set This tag sets a variable or a property to the value of an expression.
    url This tag creates a URL. The body of the url tag can have param tags for adding parameters to the URL.
    when Unlimited tags of this may exist within a choose action to provide handling for a wide range of conditions.

    You will find an example that includes several JSTL core tags here.

The following is a detail presentation of each core JSTL tag

  • catch

    The <c:catch> tag is used to catch any java.lang.Throwable exception in the body of the tag.
    The syntax is as follows:
    <c:catch [var="variable_name"]>
      JSP body 
    </c:catch>
    Attribute(s):
    Attribute Required Description
    var No Name of the variable that is set to the thrown exception
    Example:
    <c:catch var="exp">
      <c:import url="http://www.god.com/book.html"/>
    </c:catch>
    <c:if test="${not empty exp}">
      Sorry, unable to import the book from url, got exception <c:out value="${exp}" />
    </c:if>
  • choose, when and otherwise

    The <c:choose> action enables you to provide handling for a set of mutually exclusive conditions, instead of just one.
    The syntax is as follows:
    <c:choose>
      <c:when test="expression">
        JSP body 
      </c:when>
    ...
      <c:otherwise>
        JSP body
      </c:otherwise>
    </c:choose>

    Unlimited <c:when> actions may exist within a <c:choose> action to provide handling for a wide range of conditions. The <c:otherwise> action may appear only once because it covers all remaining alternatives and must therefore appear after all <c:when> actions.

    <c:when> tag attribute(s):
    Attribute Required Description
    test Yes If the test is true, it processes the tags body.
    Example:
    <c:choose>
      <c:when test="${hour >= 0 && hour <=11}">
        Good Morning!
      </c:when>
      <c:when test="${hour >= 12 && hour <=17}">
        Good Afternoon!
      </c:when>
      <c:otherwise>
       Good Evening!
      </c:otherwise>
    </c:choose>
  • if

    The <c:if> tag evaluates a test condition. If it is true, it processes its body else the body is ignored.
    The syntax is as follows:
    <c:if test="test condition">
          [var="variable name"]
          [scope="page|request|session|application"]>
      JSP body
    </c:if>
    Attribute(s):
    Attribute Required Description
    test Yes If the test condition is true, it processes its body else the body is ignored
    var No Variable that holds the result (true or false) of the test condition evaluation. This variable can be used for future test conditions (depending on the scope of the variable), thus avoiding reevaluation of the test condition.
    scope No (default=page) Scope of the variable specified in the var attribute. This can be page, request, session, or application.
    Example:
    <c:if test="${hour >= 0 && hour <=11}">
      Good Morning!
    </c:if>
    <c:if test="${hour >= 12 && hour <=17}">
      Good Afternoon!
    </c:if>
    <c:if test="${hour >= 18 && hour <=23}">
      Good Evening!
    </c:if>
  • import

    The <c:import> tag imports the content of a URL-based resource and provides a simple, generic way to access URL-based resources that can be either included or processed within the JSP page. The body of the import tag can have param tags for adding parameters to the URL.
    The syntax is as follows:
    <c:import url="resource URL"
              [var="variable name"]
              [scope="page|request|session|application"]
              [varReader="variable name"]
              [context="context name"]
              [charEncoding="character encoding"]>
              JSP body
    </c:import>
    Attribute(s):
    Attribute Required Description
    url Yes The relative or absolute URL of the resource being imported.
    var No The variable in which the content retrieved from the specified URL should be stored.
    scope No (default=page) Scope of the variable specified in the var attribute. This can be page, request, session, or application.
    varReader No Name of the Reader for the URL resource.
    context No When the URL being imported is a relative URL in another context, the context name is specified in this attribute.
    charEncoding No The character encoding of the content at the specified URL.
    Example:
    <c:import url="http://www.god.com/book.html"/>
  • forEach

    The <c:forEach> tag is used for iteration over collections.
    The syntax is as follows:
    <c:forEach [items="collection of items"]
        [begin="begin index"]
        [end="end index"]
        [step="step size"]
        [var="variable name"]
        [varStatus="status variable name"]>
      JSP body
    </c:forEach>
    Attribute(s):
    Attribute Required Description
    items No The collection of items to iterate over. The collection can be any Java object of type java.util.Collection or java.util.Map.
    begin No The start index for the iteration.
    end No The end index for the iteration.
    step No The size of the index increment while iterating over the collection. The iteration will process every step item of the collection, starting from the begin index (or first item, if begin is not specified). For example, if step is 5, the index is incremented by 5 every iteration.
    var No The name of the variable in which the current item in the iteration is stored.
    varStatus No The name of the variable in which the status of the iteration is stored. Possible status to retrieve:
    • current => Retrieves the current item in the iteration.
    • index => Retrieves the index of the current round of the iteration.
    • count => Retrieves the "count" of the current round of the iteration..
    • first => Returns true for the first item in the iteration.
    • last => Returns true for the last item in the iteration.
    • begin => Returns the value of the 'begin' attribute for the associated tag, or null if no 'begin' attribute was specified.
    • end => Returns the value of the 'end' attribute for the associated tag, or null if no 'end' attribute was specified.
    • step => Returns the value of the 'step' attribute for the associated tag, or null if no 'step' attribute was specified.
    Example:
    <ul>
      <c:forEach items="${list}" var="item">
        <li><c:out value="${item}"/></li>
      </c:forEach>
    </ul>
  • forTokens

    The <c:forTokens> tag is used for iterating over tokens separated by a delimiter.
    The syntax is as follows:
    <c:forTokens items="string of tokens"
                 delims="delimiter characters"
                 [begin="begin index"]
                 [end="end index"]
                 [step="step size"]
                 [var="variable name"]
                 [varStatus="status variable name"]>
         JSP body 
    </c:forTokens>
    Attribute(s):
    Attribute Required Description
    items Yes The string of tokens to iterate over.
    delims Yes The set of characters that separate the tokens in the string.
    begin No (default=0) The start index for the iteration.
    end No The end index for the iteration.
    step No The size of the index increment while iterating over the tokens. The iteration will process every step token, starting from the begin index (or first item, if begin is not specified).
    var No The name of the variable in which the current item in the iteration is stored.
    varStatus No The name of the variable in which the status of the iteration is stored. Possible status to retrieve:
    • current => Retrieves the current item in the iteration.
    • index => Retrieves the index of the current round of the iteration.
    • count => Retrieves the "count" of the current round of the iteration..
    • first => Returns true for the first item in the iteration.
    • last => Returns true for the last item in the iteration.
    • begin => Returns the value of the 'begin' attribute for the associated tag, or null if no 'begin' attribute was specified.
    • end => Returns the value of the 'end' attribute for the associated tag, or null if no 'end' attribute was specified.
    • step => Returns the value of the 'step' attribute for the associated tag, or null if no 'step' attribute was specified.
    Example:
    <ul>
      <c:forTokens items="${list}" delims =":" var="item" varstatus="status">
        <li>${status.count} ${item}</li>
      </c:forTokens>
    </ul>
  • out

    The <c:out> tag is used to output the result of an expression. It functions similarly to the <%= ... => JSP syntax.
    The syntax is as follows:
    <c:out value="expression"
           [default="default expression value"]
           [escapeXml="true|false"]>
        JSP body
    </c:out>
    Attribute(s):
    Attribute Required Description
    value Yes The expression to be evaluated. The result of the expression is output to the page.
    default No The default value of the expression in case it results in a null.
    escapeXml No (default=true) Determines whether the <, >, " and ' characters in the resultant string should be converted to their corresponding character entity codes.
    Example:
    <ul>
      <c:forTokens items="${list}" delims =":" var="item" >
        <li><c:out value="${item}" default="null" /></li>
      </c:forTokens>
    </ul>
  • param

    The <c:param> tag used along with the import, redirect, or url tags to add a parameter to a URL.
    The syntax is as follows:
    <c:param name="parameter name" 
             [value="parameter value"]>
       JSP body 
    </c:param>
    Attribute(s):
    Attribute Required Description
    name Yes The name of the parameter.
    value No The value for the parameter.
    Example:
    <c:import url="login.jsp">
      <c:param name="user" value="${param.loginname}"/>
      <c:param name="pass" value="${param.password}"/>
    </c:import>
  • redirect

    The <c:redirect> tag redirects the user to a new URL. The body of the redirect tag can have param tags for adding parameters to the URL.
    The syntax is as follows:
    <c:redirect url="resource URL">
      [context="context name"]>
      JSP body
    </c:redirect>
    Attribute(s):
    Attribute Required Description
    url Yes The relative or absolute URL of the resource being imported.
    context No When the URL to which one is being redirected is a relative URL in another context, the context name is specified in this attribute.
    Example:
    <c:catch var="exception">
      <c:import url="http://www.god.com/book.html"/>
    </c:catch>
    <c:if test="${not empty exception}">
      <c:redirect url="/error.jsp"/>
    </c:if>
  • remove

    The <c:remove> tag removes a variable from a scope.
    The syntax is as follows:
    <c:remove var="variable name"
              [scope="page|request|session|application"] 
              />
    Attribute(s):
    Attribute Required Description
    var Yes The variable to be removed.
    scope No (default=page) Scope of the variable specified in the var attribute. This can be page, request, session, or application.
    Example:
    <c:remove var="foobar"/>
  • set

    The <c:set> tag sets a variable or a property to the value of an expression.
    The syntax is as follows:
    <c:set [var="variable name"]
      [value="expression"]
      [target="JavaBean or Map object name"]
      [property="target property name"]
      [scope="page|request|session|application"] >
      JSP body
    </c:set>
    Attribute(s):
    Attribute Required Description
    var No (default=body content) The variable that is set to the value of the expression.
    value No The expression to be evaluated.
    target No The target object (JavaBean or Map object) whose property will be set.
    property No Name of the property to be set in the target attribute.
    scope No (default=page) Scope of the variable specified in the var attribute. This can be page, request, session, or application.
    Example:
    <c:set var="counter" value="${counter + 1}" /> 
  • url

    The <c:url> tag creates a URL. The body of the url tag can have param tags for adding parameters to the URL.
    The syntax is as follows:
    <c:url [var="variable name"]
      [scope="page|request|session|application"]
      [value="url"]
      [context="context name"]>
      JSP body 
    </c:url>
    Attribute(s):
    Attribute Required Description
    var No The variable that contains the processed URL.
    scope No (default=page) Scope of the variable specified in the var attribute. This can be page, request, session, or application.
    value No The URL to be processed.
    context No When the URL is a relative URL in another context, the context name is specified in this attribute.
    Example:
    <c:url value="http://www.google.com/search">
      <c:param name="q" value="${searchTerm}"/>
    </c:url>

Example of using core JSTL element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is CoreJSTL).

In this example, we will add two files, header.jsp and footer.jsp.

  • It is customary to place all include files, which are used several times in the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h1>Book inventory control</h1>
    <hr>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr>
    Status on <%= (new java.util.Date()).toString() %>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Footer.jsp and folder should be WEB-INF).

In this example, we will add two java files, Book.java and Library.java.

  • We need a java class with information about each book in a library.
    Here is the Book.java:
    package doc;
    
    public class Book {
    
      private String isbn_no;
      private String short_desc;
      private double price;
    
      public Book() {
        this.isbn_no = "";
        this.short_desc = "";
        this.price =0;
      }
    
      public Book(String isbn_no, String short_desc, double price) {
        this.isbn_no = isbn_no;
        this.short_desc = short_desc;
        this.price = price;
      }
    
      public String getIsbn_no() {
        return isbn_no;
      }
    
      public String getShort_desc() {
        return short_desc;
      }
    
      public double getPrice() {
        return price;
      }
    
      public void setIsbn_no(String isbn_no) {
        this.isbn_no = isbn_no;
      }
    
      public void setShort_desc(String short_desc) {
        this.short_desc = short_desc;
      }
    
      public void setPrice(double price) {
        this.price = price;
      }
    }

    For those who participate in the review: create a java class in Netbeans and replace generated code for the java file with that shown above (the java file name is Book and package should be doc).

  • We want to register isbn number, description and price of each book we have in our library (Perhaps not the most suitable properties for a book in a library, but anyway ...).
  • We need a java class to control what books we have in our library.
    Here is the Library.java:
    package doc;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Pattern;
    
    public final class Library {
    
      private List<Book> books;
      private String searchString = "";
      private List<Book> searhBooks;
    
      public int getSearchFound() {
        return searhBooks.size();
      }
    
      public String getSearchString() {
        return searchString;
      }
    
      public void setSearchString(String searchString) {
        this.searchString = searchString;
        searhBooks = new ArrayList<Book>();
        for (Book book : books) {
          if (book.getShort_desc().contains(searchString)) {
            searhBooks.add(book);
          }
        }
      }
    
      public Library() {
        books = new ArrayList<Book>();
        setAddBook(new Book("0201703092", "The Practical SQL", 39));
        setAddBook(new Book("0471777781", "Professional Ajax", 32));
        setAddBook(new Book("0764557599", "Professional C#", 42));
        setAddBook(new Book("0745675291", "XSLT Cookbook", 32));
        setAddBook(new Book("2763457534", "C++ Builder", 50));
        setAddBook(new Book("4765677213", "Servlets & JSP", 37));
      }
    
      public List<Book> getBooks() {
        return books;
      }
    
      public void setAddBook(Book book) {
        books.add(book);
      }
    
      public List<Book> getSearch() {
    
        return searhBooks;
      }
    
      public void setDelBook(String[] isbns) {
        if (isbns != null) {
          for (String isbn : isbns) {
            for (Book book : books) {
              if (book.getIsbn_no().equals(isbn)) {
                books.remove(book);
                break;
              }
            }
          }
        }
      }
    
      public boolean Validate(String test) {
        boolean result = true;
        String decimalPattern = "[0-9]+(\\.[0-9][0-9]*)*";
        result = Pattern.matches(decimalPattern, test);
        return result;
      }
    }

    For those who participate in the review: create a java class in Netbeans and replace generated code for the java file with that shown above (the java file name is Library and package should be doc).

  • In this class we have a member variable, books, which shall contain all the books in the library. This is instantiated in the default constructor. We've also added some books in this constructor.
  • Furthermore, we have a method to add new books, a method to search for books and a method of removing books from the library.
  • Finally, we have created a method that returns a list of all the books in the library.

At last we need a JSP file(s) to demonstrate how we use the JSTL core tags.

  • Here is the main JSP file:
    <%@page import="java.util.regex.Pattern"%>
    <%@page contentType="text/html" pageEncoding="UTF-8" 
            import="java.util.*, doc.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html >
    <html>
      <head>
        <title>Library Book Control</title>
        <style>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          }
          table tr.data:nth-of-type(odd) { 
            background: #eeeeee; 
          }
    
          input[type=text] {
            width: 150px;
            padding:5px; 
            border:2px solid #ccc; 
            -webkit-border-radius: 5px;
            border-radius: 5px;
            background-color: lemonchiffon;
          }
    
          input[type=text]:focus {
            border-color:#000ff;
          }
          input[type=submit], input[type=button]  {
            width: 166px;
            padding:5px; 
            background:#e0e6ff; 
            cursor:pointer;
            -webkit-border-radius: 5px;
            border-radius: 5px; 
            border: outset #333 2px;
            margin-top: 10px;
          }
        </style>    
      </head>
      <body >
    
          <div style="width:400px; margin-left: 40px;">
            <c:import url="/WEB-INF/header.jsp" />
            <jsp:useBean id="library" class="doc.Library" scope="session"/>
            <jsp:useBean id="newBook" class="doc.Book" scope="page"/>
            <c:choose>
              <c:when  test="${param.action == 'add'}">
                <c:set target="${newBook}" property="isbn_no" value="${param.isbn_no}"/>
                <c:set target="${newBook}" property="short_desc" value="${param.short_desc}"/>
                <c:if test="${library.Validate(param.price)}">
                  <c:set target="${newBook}" property="price" value="${param.price}"/>
                </c:if>
                <c:set target="${library}" property="addBook" value="${newBook}"/>
              </c:when>
              <c:when test="${param.action == 'del'}">
                <c:set target="${library}" property="delBook" value="${paramValues.isbns}"/>
              </c:when>
              <c:otherwise>
                <c:url value="search.jsp" var="searcString" >
                  <c:param name="search" value="${param.searchTerm}" />
                </c:url>
              </c:otherwise>
            </c:choose>
            <!-- List all books in the Library -->
            <h3>Library book list</h3>
            <form action='index.jsp' method="POST">
              <table  >
                <tr class="gradientdown">
                  <th style="width:100px;">Isbn_no</th> 
                  <th style="width:150px;">Short desc</th> 
                  <th style="width:70px;">Price</th> 
                  <th style="width:60px;">Del.</th>
                </tr>
                <c:forEach items="${library.books}" var="listBook" >
                  <tr class="data" >
                    <td><c:out value="${listBook.isbn_no}" /></td>
                    <td><c:out value="${listBook.short_desc}" /></td>
                    <td style="text-align: right;"><c:out value="${listBook.price}" /></td>
                    <td style="text-align: center;">
                      <input type="checkbox" name="isbns" value="" /></td>
                  </tr>
                </c:forEach>            
              </table>
              <input type="hidden" name="action" value="del"/>
              <input type="submit"  value="Delete book(s)" />
            </form>   
            <!-- Input form for a new book -->
            <hr>
            <h3>Add a book to Library</h3>
            <form action='index.jsp' method="POST">
              <table>
                <tr>
                  <td><input type="text" name="isbn_no" placeholder="Isbn number" /></td> 
                </tr>
                <tr>
                  <td><input type="text" name="short_desc" placeholder="Short desc."/></td> 
                </tr>
                <tr>
                  <td><input type="text" name="price" placeholder="Price" /></td> 
                </tr>
              </table>
              <input type="hidden" name="action" value="add"/>
              <input type="submit"  value="Add book" />
            </form>
            <hr>
            <h3>Search for book(s)</h3>
            <form id="searchForm" action='index.jsp' method="POST">
              <input type="text" id="searchTerm" name="searchTerm" placeholder="searchTerm" value="${param.searchTerm}"/><br>
              <input type="hidden" name="action" value="search"/>
              <input type="submit"  value="Search" />  
              <input type="button"  value="Clear search" 
               onclick="document.getElementById('searchTerm').value=''; document.getElementById('searchForm').submit()"/>       
            </form>
    
            <c:if test="${param.searchTerm!='' && param.searchTerm!=null}">
              <c:import url="${searcString}" />
            </c:if>
            <c:import url="/WEB-INF/footer.jsp" />
          </div>
      </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is index).

How does this work?

  • We have created two JSP files, Header.jsp and Footer,jsp, which we have included in the index.jsp file with <c:import> tag.
  • With the useBean action element we have created an instance of our library and an instance for a new book to be added. The library is made session related while the new book is only related to the life of the page.
  • With use of the JSTL core and EL syntax we test for one of three action, delete on or more books from the library, add a new book to the library or search for books by title. We use the <c:set>, <c:if>, <c:url> and <c:param> tags to handle all of those actions.
  • Because we want to use the library class and the book class, we need to import these classes with the page directive (doc.*).
  • When any delete or add is finish we uses JSTL core and EL syntax to list all the books that exists in the library with the possibility to delete any of them.
  • Furthermore, we have created a form to add a new book to the library.
  • Finally, we have created a section where we list all the books as a result of a search. In this section we import a file using <c:import> tag, which performs the task.
  • Here is the file that lists all the books, which we did a search for.
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:set target="${library}" property="searchString" value="${param.search}" />
     <hr>
     <h3>Books found in search: ${library.searchFound}</h3>
    <table  >
      <tr class="gradientdown">
        <th style="width:100px;">Isbn_no</th> 
        <th style="width:150px;">Short desc</th> 
        <th style="width:70px;">Price</th> 
      </tr>
      
      <c:forEach items="${library.search}" var="sBook" >
        <tr class="data" >
          <td><c:out value="${sBook.isbn_no}" /></td>
          <td><c:out value="${sBook.short_desc}" /></td>
          <td style="text-align: right;"><c:out value="${sBook.price}" /></td>
        </tr>
      </c:forEach>            
    </table>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is search).

  • In this file, we expect a parameter, search, that includes search-string.
  • The search-string are sent to setSearchString method in our library that performs the scan and stores it in an object.
  • We will then use a <c:foreach> tag to list all the books that satisfy your search.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>CoreJSP</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>CoreJSP</servlet-name>
            <url-pattern>/CoreJSP</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>CoreJSP</welcome-file>
        </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url CoreJSP. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /CoreJSTL) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/CoreJSTL</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.