Session改为request后显示null,这是为什么?
时间: 2023-05-22 19:02:26 浏览: 123
可能是因为您在使用Session时,没有正确绑定到请求(request)上,导致获取到的值为null。请确保您正确使用session_start()函数,并在每次请求时重新绑定session。另外,也可以检查一下您的代码逻辑,确保Session的值没有被意外地清空或重置。
相关问题
Session改为request跳转后为什么是NULL?
可以使用python3内置的math模块中的pow()函数来求立方根,即:
import math
cube_root = math.pow(27, 1/3)
print(cube_root)
# 输出 3.0
用java怎么写 var session = HttpContext.Current?.Session; var applyUserId = session[WebApplyUser.SessionName] is Dictionary<string, string> webUser && webUser.ContainsKey(ConstUserFieldNames.UserId) ? webUser[ConstUserFieldNames.UserId] : null;
这段代码是 C# 代码,不是 Java 代码,但我可以帮您将其转化为 Java 代码,您可以参考以下代码实现:
```
HttpSession session = request.getSession(false);
String applyUserId = null;
if (session != null) {
Dictionary<String, String> webUser = (Dictionary<String, String>) session.getAttribute(WebApplyUser.SessionName);
if (webUser != null && webUser.containsKey(ConstUserFieldNames.UserId)) {
applyUserId = webUser.get(ConstUserFieldNames.UserId);
}
}
```
上述代码做了以下转化:
1. 将 `HttpContext.Current` 改为了 `request.getSession(false)`,其中 `request` 是 `HttpServletRequest` 对象。
2. 将 C# 中的 `?.` 改为了 Java 中的 `!= null`。
3. 将 `session[WebApplyUser.SessionName]` 改为了 `session.getAttribute(WebApplyUser.SessionName)`,其中 `WebApplyUser.SessionName` 是 session 中存储的键名。
4. 将 `is` 改为了 `instanceof`,同时将 `Dictionary` 改为了 `Map`。
5. 将 `?` 改为了 `!= null`。
6. 将 `:` 改为了 `?`。
希望这可以帮助到您。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)