Resolves a bug_java that Tomcat 6 and 7 exists in El expression parsing

Source: Internet
Author: User

Today, when I did a data paging display, I ran into a problem that was tested and proved to be a bug in Tomcat 6, and I used the version: apache-tomcat-6.0.36, and 7.0.30 can be reproduced. Here's a detailed description of the bug:

The bug was found in the jstl<c:foreach> tag and was later analyzed as a problem with the implementation of an El expression. There is a list in the JSP page that needs to be traversed, the type of the list is Arraylist<string>, and the data I put in it is (in the form of an array for my convenience): ["1", "...", "4", "5", "6", "7", "8", " ... "," 10 "], this is a very common pager with page numbers. I used the following code to show this data:

Copy Code code as follows:

<c:foreach var= "Looper" items= "${pagehelper.pagelist}" >
<c:choose>
<c:when test= "${looper eq Pagehelper.pagedot}" >
<p> paging cursor Dot dot </p>
</c:when>
<c:when test= "${looper eq Pagehelper.pageno}" >
<p> current page is ${looper} page </p>
</c:when>
<c:otherwise>
<p> Paging Cursor:${looper}</p>
</c:otherwise>
</c:choose>
</c:forEach>

Here Pagehelper is the paging component, where the Pagedot is preset "...", PageNo is the current page number (assuming 6), and other cases show the paging cursor directly. Only the most basic conditional judgment statement is used in the loop traversal, because the pagelist has clearly stated that it is list<string&gt in the definition, and the EQ is based on the string, but it is abnormal:
Copy Code code as follows:

Javax.el.ELException:Cannot convert ... of type class java.lang.String to class Java.lang.Long

Why does a "type conversion error" occur? By analyzing the direction of the code, when the loop is entered, the first data in the list is "1", The Pagehelper.pageno is a long, at which point the El Expression parser of Tomcat converts the Looper type to a long instead of converting the Pagehelper.pageno type to string for comparison, and when traversing to the next element, Looper = "..." When the type of looper has been determined, and Tomcat tries to convert Looper to a long type when compared, and then it goes wrong.

For this reason I wrote an example code specifically:

Copy Code code as follows:

<c:foreach var= "Looper" items= "${pagehelper.pagelist}" >
<c:choose>
<c:when test= "${looper eq Fn:trim (Pagehelper.pagedot)}" >
<p> paging cursor Dot dot </p>
</c:when>
<c:when test= "${looper eq Fn:trim (Pagehelper.pageno)}" >
<p> current page is ${looper} page </p>
</c:when>
<c:otherwise>
<p> Paging Cursor:${looper}</p>
</c:otherwise>
</c:choose>
</c:forEach>

Quite simply, each time you compare the latter with the Fn:trim method to remove the left and right invisible characters. The equivalent of casting to string type, at which time Tomcat can parse the code normally, without an error.

The same set of code, which I deployed to resin, found that it worked correctly before or after modification, and that it was a Tomcat bug.

Sample code: Click to download

Demo address for Tomcat error:/bug/show.do

Ways to avoid this bug demo address:/bug/avoid.do

The above address may need to be preceded by a project name (depending on how you deploy the project)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.