java - 将 ServletContext 属性的内容保存在 int 变量中
问题描述
在第一个 JSP 中,我创建了一个 servletcontext,在其中放置了一个具有 num 值的 Count 属性,在第二个 JSP simple 中,我从该上下文中获取了 Count 值,但它给了我错误。如何将 servletcontext 类型的对象保存在数组中?
jsp1:
<%!int num=0;%>
<%ServletContext cont = getServletConfig().getServletContext();
num++;
cont.setAttribute("Conta",num);
%>
jsp2:
<% ServletContext cont = getServletConfig().getServletContext();
int contator=Integer.parseInt(cont.getAttribute("Conta"));
%>
解决方案
Hard to say without the error, but I think I may have found it.
You are hitting an error when Integer.parseInt(String) receives an int/Integer, which is throwing most likely a method not found error. In this case [void setAttribute(String, Object)] will take the integer you are passing it and [Object getAttribute(String)] will return the integer. Integer.parseInt(String) will then fail.
If you refactor to not do the Integer.parseInt() and just cast the result to the int, you should be OK.
推荐阅读
- angular - AWS EC2 实例正在运行,但我无法连接
- javascript - 定义所有属性时无法读取未定义 React 的属性“绑定”
- knockout.js - 为什么在表行中使用 data-bind ='if' 时会出现 Knockout js 的 ReferenceError
- amazon-web-services - AWS Kinesis 流检查点
- spring-boot - 无法在启用动态端口的 AWS ECS 上正确注册 Eureka 服务
- spring-boot - Spring Boot 不在 Azure WebApp 和 Skype 上显示机器人响应
- java - Java SWT:22 MB 的 runnable.jar 文件。可以减小尺寸吗?
- rest - 使用 Lumen 和 NeoEloquent OGM 从 Neo4j 检索 DateTime
- azure-resource-manager - 资源未定义
- laravel - 使用 JavaScript 查看 laravel 会话