jsp-config in web.xml

I’m using Apache Tomcat 6.0.13 which implements Servlet 2.5 and JSP 2.1 specification. As I outlined earlier I’m using Stripes web application framework and JSPs and JSP tag files for rendering the page. With this combination I’m quite satisfied, because it enables me quick and efficient development.

Additionally I discovered the <jsp-config> configuration possibilities in the web.xml file. There you can configure the behavior of a group of JSP files. For instance I use the <page-encoding> and <trim-directive-whitespaces> configuration for all JSP files.

Page encoding I set to “UTF-8″ as default. In general it is a good suggestion to use UTF-8 from the beginning on with your web projects on any layer (database, application server, front-end). Because you will have less problems with character sets and special characters when it comes to internationalization and localization.

Trim white spaces is new in the JSP 2.1 specification, and very useful. When set to true, it removes all empty lines in the output before returning it to the browser. This makes your HTML more readable. Unfortunately it does not remove the empty lines in the JSP tag files :-(

So my web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <servlet>
        ...
    </servlet>

    <servlet-mapping>
        ...
    </servlet-mapping>

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>
                true
            </trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>
</web-app>

2 Responses to “jsp-config in web.xml”

  1. Pfosten Says:

    Hallo!

    Jaja Mexx! So bin ich auch mal in den Genuss deinen Blog’s gekommen. Hab den Link dazu auf der sfv.at Page gefunden und war schon ein bisschen überrascht.

    Naja … hätt ich damal in der Schule besser aufgepasst, könnte ich jetzt auch ein bisschen Java. Hat mich aber nicht gefreut und so bin ich nur auf einem ‘HTML-Standard’ mit ein bisschen CSS.

    Bei PHP bin ich dann auch schon ausgestiegen und so ists nie zu Java gekommen. ;)

    Egal sehn uns heute ja sowieso und wenn nicht, dann morgen (gegen Großgmain).

    okok
    lg Pfosten

  2. Jobst Says:

    Hi Markus,

    thanks for this post.
    It was very helpful for me !

    Greetings, Jobst ;-)

Leave a Reply