JavaServer Pages Examples


You can download all the book examples for the 3rd edition, including source code for all custom actions and beans, and install locally. All you need is included in this file: jspbook3.zip. To run the examples on your own desktop or server, you need a Servlet 2.4/JSP 2.0 compliant container, such as Apache Tomcat 5.0.12 or later. 

 

Note! If you’re looking for the examples for earlier editions of the book, use these links:

  1. 1st edition jspbook.zip
  2. 2nd edition jspbook.zip
How to install the examples for Tomcat, or any other compliant web container, is described in detail in the book. Briefly, this is what you need to do to run the examples:

  1. Download the examples ZIP file and save it on your hard disk.
  2. Unpack the contents of the file with either a ZIP utility program (such as WinZip) or the jar command that’s included in the standard Java environment. The file contains two directories: ora and src.
  3. The ora directory contains the book examples arranged in the structure specified by the Servlet 2.2 specification for web applications archives (WAR files), which is supported by most containers also as an open file system structure. For Tomcat, just copy the ora directory and all its subdirectories under Tomcat’s webapps directory. For other containers, see their documentation for how to install a web application.To run the database examples, you need to adjust the JDBC settings in the ora/WEB-INF/web.xml, install a JDBC driver for your database, and create the example tables. See the README file and the book for details.
  4. Restart the container and access the main example page with a URL like: http://localhost:8080/ora/

8 thoughts on “JavaServer Pages Examples

  1. Greg Bologna on said:

    Hans, where do I find jdbc20_stdext_classes.jar? Thanks.

  2. Hi Greg,

    The JAR file is included in the WEB-INF/lib directory for the example application for the 1st and 2nd edition of the book, but with a current Java version it is no longer needed because these classes are now included in the Java core, in package javax.sql.

    Best Regards,
    Hans

  3. Kemin Zhou on said:

    I am going through the book examples in 3rd Edition, at page 279, this examples does not work because of proxy settings. The program stuck at <c:import url="http….
    I have worked with Java source code dealing with this type of issues before, I know the exact lines of code needed to fix this problem. Is there a proxy setting in JSP too? I could not change the underlying library code.

    • JSP does not in itself provide any proxy settings, but you can use some other web server component type to initialize proxy settings. For instance, you can create and register a ServletContextListener and do the initialization in its contextInitialized() method. I have just moved and have not unpacked my books yet so I cannot point to the chapter that describes ServletContextListener’s in my book (but it should be in the last part, probably in a chapter about database access). This article, however, should give you a start:

      http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

      I hope this helps,
      Hans

  4. How can I use Javabean methods from JSP that works with just one element, specified by an index?

    public int getCategories(int i) {
    return categories[i];
    }
    public void setCategories(int i, int category) {
    this.categories[i] = category;
    }

    • A JavaBean getter method does not take any argument, so your methods are not JavaBean methods. But you can access the elements of an instance of List using the JSP Expression Language, e.g. ${categories[1]}. I describe this in my JSP book and if you search the net, I’m sure you can find a lot of helpful articles.

      Best Regards,
      Hans

      • Quote from book, chapter 20.1.1 JavaBeans Naming Conventions:

        “Besides simple single-value properties, beans can also have multivalue properties
        represented by an array of any type. This is called an indexed property in the
        specification. Two types of access methods can be used for an indexed property:
        methods reading or writing the whole array or methods working with just one
        element, specified by an index:
        public int[] getCategories( ) {
        return categories;
        }
        public void setCategories(int[] categories) {
        this.categories = categories;
        }
        public int getCategories(int i) {
        return categories[i];
        }
        public void setCategories(int i, int category) {
        this.categories[i] = category;
        }”

        • Hi Ivan,

          Ah, you’re right! I don’t remember if indexed properties have always been part of the JavaBeans spec or if they were added later, but since the Expression Language has never supported this type of property, I may just have forgot they were there. But as I recall, the EL allows access to array elements (in addition to Collection elements) so you should be able to use the getter method that returns the array and then pick an element using the normal EL index syntax, e.g. ${myBean.categories[myIndex]}.

          Note I haven’t tested this and I haven’t worked with JSP/EL in a few years, so I may be wrong.

          Best Regards,
          Hans

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>