可以在JSP scriptlet中使用Java Lambda表达式吗?

时间:2016-04-25 06:02:43

标签: java jsp servlets lambda scriptlet

我需要在JSP源文件中执行以下Java代码。我首先创建一个Map对象:

Map<String,String> data = new HashMap<>(6);
data.put("Java version", "java.version");
data.put("Java home directory", "java.home");
// and so on until it's full

Map实例填充数据后,我尝试使用以下lambda表达式检索并显示所述数据:

final JspWriter out2 = out;
data.forEach((m,n) ->
{
    try
    {
        out2.println(m + ", " + n + "<br />");
    }
    catch(IOException ex) {}
});

我从上面的两个代码块中得到2个例外。第一个错误来自第一个块,当我声明Map实例时:

PWC6197: An error occurred at line: 27 in the jsp file: /index.jsp
PWC6199: Generated servlet error:
diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)

我可以通过用这样的完整形式替换菱形运算符来重写第一个块的第一行,从而轻松解决这个问题:

Map<String,String> data = new HashMap<String,String>(6);

对于第二个块,报告的错误消息为:

PWC6197: An error occurred at line: 27 in the jsp file: /index.jsp
PWC6199: Generated servlet error:
lambda expressions are not supported in -source 1.5
  (use -source 8 or higher to enable lambda expressions)

现在,我可以通过简单地重写那段Java代码来解决这个错误,将其替换为执行相同任务但不使用lambda表达式的东西。但这将是一种解决方法,我更倾向于使用Java8功能。基于我收到的错误消息,在我看来,在编译过程的某个地方,Java错误地使用JDK 1.5而不是1.8。也许其中一个GlassFish库是使用JDK 1.5编写的,因此强制我的整个代码符合JDK版本?我真的不知道如何编译我的Java8代码。

我当前安装的JDK是版本1.8.0_77。我正在使用NetBeans 8.1与GlassFish Server 4.1.1一起开发所有这些。在NetBeans中,我确保检查ProjectProperties =&gt; Sources =&gt; Source / BinaryFormat是否为此项目设置为JDK8。

注意:这两条错误消息前面都有以下行,我将其编辑出来以使其更具可读性:

org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6199: Generated servlet error:
source value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error:
target value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error:
To suppress warnings about obsolete options, use -Xlint:-options.

这是完整的GlassFish服务器日志:

Severe:   Error compiling file: C:\Users\Server\GlassFish_Server\glassfish\domains\myLocalDomain1\generated\jsp\hwk5\org\apache\jsp\index_jsp.java
Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
PWC6199: Generated servlet error:
source value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
target value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
To suppress warnings about obsolete options, use -Xlint:-options.
PWC6197: An error occurred at line: 27 in the jsp file: /index.jsp
PWC6199: Generated servlet error:
diamond operator is not supported in -source 1.5
  (use -source 7 or higher to enable diamond operator)
PWC6197: An error occurred at line: 27 in the jsp file: /index.jsp
PWC6199: Generated servlet error:
lambda expressions are not supported in -source 1.5
  (use -source 8 or higher to enable lambda expressions)
    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:129)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:299)
    at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:392)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案