记录java.util.Map.getOrDefault()方法导致JSP编译失败异常:Unable to compile class for JSP;
一、报错信息:
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 114 in the jsp file: /AICSSO/DZZYSSO.jsp
The method getOrDefault(String, String) is undefined for the type Map<String,Object>
111: JSONObject jsStr = JSONObject.parseObject(result);
112: System.out.println("jsStr =========================== " + jsStr);
113: Map<String, Object> map = JSONObject.toJavaObject(jsStr,Map.class);
114: String userId =(String) map.getOrDefault("msg", "xxx");
115: System.out.println("闂ㄦ埛楠岃瘉鎺ュ彛鍝嶅簲鏁版嵁锛歶serId = " + userId);
116: if (userId.equals(sid)){
117: //璁よ瘉鎴愬姛锛�
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.ufgov.midas.pt.common.CharsetFilter.doFilter(CharsetFilter.java:63)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.24 logs.
二、报错原因分析
原文代码报错位置:
Map<String, Object> jsStr = JSONObject.parseObject(result);
System.out.println("jsStr =========================== " + jsStr.toString());
// TODO: 2024-03-01 报错jsp编译失败 TODO: map.getOrDefault() 报错
String userId =(String) map.getOrDefault("msg", "xxx");
起初是找不到异常位置,我面经过排除才找到是因为 java.util.Map.getOrDefault()方法!
后面一直找不到原因!后面经过查询这个方法才知道,该方法是 Java 8新增加的方法。而集成的项目是一个老项目,jdk版本是低于1.8的。前期在写 集成代码 jsp文件的时候是在本地环境写的。所有本地是没有出现jsp编译错误!
【下次注意:本地环境没出错,线上出错,一定要考虑到环境配置问题!!】