I found here the following article: Lambda expressions in JSP files will not compile
So I added compilerSourceVM and compilerTargetVM to conf/web.xml in Tomcat. (not in the project web.xml).
So in general I can use stream, but for (some?) lambda expression the JSP will not be compiled by tomcat.
The following are examples. I want get the lambda working, I'm not looking for better coding in first example.
Simple example (working):
<p>
Cookies:<br />
<%
Cookie[] cookies = request.getCookies();
if ( cookies == null || cookies.length == 0 ) {
out.println("No cookies found<br/>");
} else {
out.println("Cookies found size=" + cookies.length + "<br/>");
List<Cookie> cList = Arrays.stream(request.getCookies()).collect(Collectors.toList());
for ( Cookie c : cList ) {
out.println("C: " + c + "<br/>");
}
out.println("Cookies found size=" + cookies.length + "<br/>");
}
%>
</p>
Simple example (not working):
Instead to have a for loop and collecting in a list, I will use the Stream::forEach with the lambda expression like:
Arrays.stream(request.getCookies()).forEach(cookie -> {out.println("Cookie information here<br/>");});
In Eclipse the JSP will be compiled and don't show some errors.
Deploying to Tomcat and calling the URL, I'm getting:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [53] in the jsp file: [/report.jsp]
Unhandled exception type IOException
50: } else {
51: out.println("Cookies found size=" + cookies.length + "<br/>");
52: Arrays.stream(request.getCookies()).forEach(cookie -> {
53: out.println("Cookie information here<br/>");
54: });
Any hints?
Thanks in advance.
Edit:
I switched to Tomcat 9. Same problem. I found also some hints, that Java 1.8 + stream + lambdas should work in Tomcat8+9.
So I changed the lambda expression like:
Arrays.stream(request.getCookies()).forEach(cookie -> {System.out.println("Cookie information here<br/>");});
So I replaced "out.println" to "System.out.println". Now the stream and lambda are working. Println going to console as expected. Also added some ".filter". Also working fine.
Now the question. Why I can't use out.println or out.write in the lambda expression. In the same JSP the out.println is working in the for-each loop showing in the example above. Any idea?
Edit:
Found the next problem, this is pure Java problem, so when you edit a JSP in Eclipse. Eclipse is not showing this problem in the editor.
a.) out.write/out.println will throw a checked exception, so I need a try/catch, because lambda's not allow a checked exception.
b.) out Variable must be final. So you need a final JspWriter myOut = out;
c.) You may have to import the JSP API lib into our project. Gradle example: providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api:2.3.0'
Can you try adding the below init parameters in your web.xml,
<init-param>
<param-name>compilerSourceVM</param-name>
<param-value>1.8</param-value>
</init-param>
<init-param>
<param-name>compilerTargetVM</param-name>
<param-value>1.8</param-value>
</init-param>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.