gpt4 book ai didi

java - BlobstoreService 的 HttpSession Null

转载 作者:行者123 更新时间:2023-12-01 15:29:38 24 4
gpt4 key购买 nike

在我的 Google App Engine 应用程序中,登录用户可以上传他们的照片及其其他详细信息。我使用 HttpSession 来管理日志记录。当登录用户填写表单并选择要上传的照片并单击“保存”按钮时,由于 Session 为空,因此会引发 NullPointerException 。 (数据存入数据库,但数据存入数据库后尝试转发成功页面时抛出NullPointerException)。

Form action = `blobstoreService.createUploadUrl("/users/adddetails")`

我已将 System.out.println(hs.getAttribute("logUserName") + ""); 放在 servlet 之上,单击后会触发保存按钮。输出为null

如何避免这种情况?

已更新

登录用户填写表单后,转到我在问题中提到的 (/users/adddetails) 路径并将数据添加到数据库。它工作正常。添加数据后,使用 resp.sendRedirect() 方法重定向到另一个 servlet。在该 servlet 上,我正在检查 Session 是否为 null,如下所示。

try 

{
HttpSession hs = req.getSession();

if(!hs.getAttribute("logUserName").equals(null)) {
RequestDispatcher d = req .getRequestDispatcher("/WEB-INF/userhome/add_data.jsp");
d.forward(req, resp); }

else {
RequestDispatcher d = req .getRequestDispatcher("user_login.jsp");
d.forward(req, resp);
}

} catch (NullPointerException ex) {

// NullPointerException thrown from here

RequestDispatcher d = req.getRequestDispatcher("user_login.jsp");
d.forward(req, resp);
}catch (Exception ex) {
RequestDispatcher d = req.getRequestDispatcher("/msg/exception.jsp");
d.forward(req, resp);
}

NullPointerException 是从上面代码中的 catch block 抛出的。 logUserName 是我用来从客户端到服务器传递用户名的参数。

最佳答案

请尝试以下代码。

HttpSession hs = req.getSession(false);
if(hs == null)System.out.println("session is null");
else {
String name = hs.getAttribute("logUserName");
if(name == null)System.out.println("name is null");
else System.out.println(name);

关于java - BlobstoreService 的 HttpSession Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9713555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com