Configuring ELS2C Home « Configuring EL

Although we will not study the JavaServer Pages Standard Tag Libraries (JSTL) until the next section, when JSTL is used in conjunction with EL and JavaBeans Standard Actions, we can write completely scriptless JSP pages. We can also enforce the writing of scriptless JSP pages by using the DD to disable scripting in a set of JSP pages or indeed all JSP pages.

Conversely, you may want to disable EL within your pages and we can also configure the DD to do just this or use a page directive to do the same thing.

In our final lesson on EL we look at the different ways we can configure our pages to allow scriptless JSP pages and also to disable EL evaluation within JSP pages.

Scriptless JSP PagesTop

We enforce the writing of scriptless JSP pages by using the DD to disable scripting in a set of JSP pages or indeed all JSP pages. This is achieved using the <jsp-property-group> sub-level element of the top-level <jsp-config> element. The example below will disable java scripting in all JSP pages within a web application:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
   <jsp-config>
      <jsp-property-group>
         <url-pattern>*.jsp</url-pattern>
         <scripting-invalid>true</scripting-invalid>
      </jsp-property-group> 
   </jsp-config>
</web-app>

The <jsp-config> ElementTop

Ok lets look at the DD elements we have entered starting with the top-level <jsp-config> element which is used to define global configuration information in a web application for JSP files.

The <jsp-property-group> sub-level element is optional and groups together JSP pages to be given global properties as defined by the sub-elements within it and has one mandatory sub-level element <url-pattern> and eight optional sub-level elements. The <url-pattern> sub-level element which is mandatory, can appear once or many times and works the same as all the other URL pattern matchers we have seen. The<scripting-invalid> sub-level element is optional, can appear zero or one times and controls whether scripting elements are invalid in a group of JSP pages.

If we were to create a WEB-INF directory within Tomcat and save the above DD as web.xml and the code below as expr16.jsp within the application root, and then try to run the code we would get an exception.


<%-- Stopping Java Scripting in JSP pages --%>
<%
    pageContext.setAttribute("name", "charlie", PageContext.PAGE_SCOPE);
%>
<%-- Using pageScope Implicit Object With EL --%>
${pageScope.name}
${pageScope["name"]}

The above scenario produces an exception as shown in the following screenshot:

run expr16jsp JSP

Disabling EL EvaluationTop

There are also situations in which you may not want EL evaluated, for example when you are using a JSP 2.0 or later container but need to deploy applications within it which are JSP 1.2 or earlier, as these applications won't recognize EL.

There are two ways we can disable EL evaluation within our applications. We briefly covered the first when we summarized Page Directive Attributes in the JSP Directives lesson. The second way to disable EL is once again via the DD using the <jsp-property-group> sub-level element of the <jsp-config> element, but this time we set the <el-ignored> sub-level element to true to achieve this.

The DD example below will disable EL for the expr16.jsp JSP page we wrote above, remember that we put this within the application root.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
   <jsp-config>
      <jsp-property-group>
         <url-pattern>/expr16.jsp</url-pattern>
         <el-ignored>true</el-ignored>
      </jsp-property-group> 
   </jsp-config>
</web-app>

The above code produces a screen that looks like the following:

run expr16bjsp JSP

As you can see from the above screenshot The ${ denotation for the start of an EL expression has been ignored and the lines are treated as part of a string. You could also exclude EL and scripting from JSP pages if you wished there is no mutual exclusion.

For more details on the other DD sub-level elements not shown in this lesson take a look at the top-level <jsp-config> element within the quick reference section of the site.

Lesson 5 Complete

In this lesson we finished our study of the Expression Language by looking at how we can configure our pages for script-free EL or non-EL use.

What's Next?

That's it for EL in the next section we start our investigation of the JavaServer Pages Standard Tags Library (JSTL).

go to home page Homepage go to top of page Top