Tuesday, August 8, 2017

TLD error: attribute items does not accept any expressions


If you just upgrade Servlet from 2.3 to 2.4 and after that you encounter this error:

    According to TLD or attribute directive in tag file, attribute items does not accept any expressions.

It may be because that (Quoted from https://docs.oracle.com/cd/E19316-01/819-3669/bnajh/index.html):

Each JSP page has a default mode for EL expression evaluation. The default value varies depending on the version of the web application deployment descriptor. The default mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate EL expressions; this automatically provides the default that most applications want. The default mode for JSP pages delivered using a descriptor from Servlet 2.3 or before is to ignore EL expressions...

To quick-fix this issue caused by the upgrade from Servlet 2.3 to 2.4, we can simple add this configuration in web.xml to change the behavior of Servlet 2.4 to be backward compatible with Servlet 2.3:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <el-ignored>true</el-ignored>
    </jsp-property-group>
</jsp-config>



Sunday, August 6, 2017

J2EE: custom error page


If a custom error page for a specific HTTP error is needed:

1. Add the error page configuration in the web application's WEB-INF/web.xml:
     <error-page>
        <error-code>404</error-code>
        <location>/error404.html</location>
    </error-page>


2. Create a error404.html at the web application's root directory.

If such configuration is not set in the web application but somehow the 404 error can still trigger a custom error page, that may be because the administrator has done it for you in the Tomcat's conf/web.xml.


Saturday, August 5, 2017

JSP: error page for uncaught exception


To create an error page to print out the details of an uncaught exception:

1. In WEB-INF/web.xml, add the error-page element:
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/error.jsp</location>
    </error-page>


2. Create the error.jsp in the root directory with the following content:
    <%@ page isErrorPage="true" import="java.io.*" contentType="text/plain"%>

    Exception:
    <%
        PrintWriter printWriter = new PrintWriter(out);
        exception.printStackTrace(printWriter);
        printWriter.close();
    %>


However, for security reason, you don't want to leak the information of the stack trace of your exception to a client side browser, because it is a trace of your source code. The above error.jsp that prints out the stack trace should be used only in the development environment. In a production environment, an error.jsp may contain only an error message such as "An error has occurred. Please contact ..."
 
Get This <