Tomcat7出现HTTP Status 500 - java.lang.ClassCastException: org.apache.jasper.el.ELContextImpl cannot b...

本文描述了在使用Tomcat7部署WAR包后遇到无法访问localhost:8080的问题,并通过调整context.xml文件中的配置解决了EL表达式错误导致的问题。通过加入指定的XML元素和重新启动服务,最终成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在Tomcat7上发布了一个war,过一阵子发现localhost:8080都进不去了。在浏览器输入http://localhost:8080出现如下内容:

几天折磨下来,看到bug就头痛,可是没办法啊,还是得想办法解决。

Google百度一番,终于得解。

如下:::

里面说是EL表达式的错误,个人呢觉得方案2比较好用,

于是修改C:\Tomcat7\conf\context.xml如下:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <Loader delegate="true" />

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

加入了上面的粗体字后,停止Tomcat7服务,在启动Tomcat7服务,OK!!!

 

 

转载于:https://www.cnblogs.com/kuangyuping/p/3658406.html

### 问题解析 `java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String` 是由于尝试将 `HashMap` 对象强制转换为 `String` 类型所引发的运行时异常。这种错误通常发生在数据类型不匹配的情况下,尤其是在处理集合框架(如 List 或 Set)时未正确指定泛型或误用了对象类型的场景下。 以下是针对该问题的具体解决方案: --- ### 解决方案 #### 方法一:确认并修正泛型定义 如果程序中使用了未经泛型限定的集合类(如 `List`),可能导致存储的数据类型与预期不符。例如,当一个 `List<HashMap>` 被当作 `List<String>` 使用时,就会触发此异常[^2]。 确保在声明集合变量时明确定义其泛型类型,例如: ```java // 正确方式:显式指定泛型类型 List<Map<String, Object>> list = new ArrayList<>(); ``` 这样可以避免因类型推断错误而导致的隐式转换问题。 --- #### 方法二:安全地访问集合元素 在遍历集合前,应先验证其中的实际数据类型是否符合预期。可以通过 `instanceof` 关键字进行类型检查后再执行强转操作。例如: ```java Object obj = list.get(i); if (obj instanceof Map) { Map<String, Object> map = (Map<String, Object>) obj; // 安全地访问 Map 中的内容 } else { throw new IllegalArgumentException("Unexpected type found in the list."); } ``` 上述代码片段能够有效防止非法类型转换的发生[^5]。 --- #### 方法三:序列化/反序列化替代直接转换 对于复杂对象(如 `HashMap`),建议采用 JSON 序列化工具将其转化为字符串形式再进一步处理。这种方式不仅规避了潜在的类型冲突风险,还增强了代码的可读性和维护性[^4]。示例实现如下: ```java import com.google.gson.Gson; Gson gson = new Gson(); for (int i = 0; i < list.size(); i++) { String jsonString = gson.toJson(list.get(i)); System.out.println(jsonString); try { JSONObject jsonObject = new JSONObject(jsonString); System.out.println(jsonObject.getString("keyName")); } catch (JSONException e) { e.printStackTrace(); } } ``` --- #### 方法四:调整 Redis 数据存取逻辑 如果是基于 Redis 的应用场景,则需注意哈希表结构内的值类型一致性。若某些字段可能包含不同基础类型(如整数、布尔值等),则统一将其映射至字符串表示更为稳妥[^3]。具体做法见以下代码段: ```java import org.springframework.beans.BeanUtils; import cn.hutool.core.bean.copier.CopyOptions; import java.util.Map; public static void main(String[] args) { UserDTO userDTO = ... ; // 初始化 DTO 实体 // 将实体属性复制到 Map 并自动完成 toString 处理 Map<String, Object> userMap = BeanUtils.beanToMap( userDTO, new HashMap<>(), CopyOptions.create() .setIgnoreNullValue(true) .setFieldValueEditor((field, value) -> value != null ? value.toString() : "") ); redisTemplate.opsForHash().putAll("loginUser:" + userId, userMap); } ``` --- ### 总结 以上四种策略分别从根源控制、运行防护以及实际应用层面提供了应对措施。开发者可根据具体情况选用最合适的手段解决问题。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值