Jsp 如何仅在对象不存在时创建对象?

Jsp 如何仅在对象不存在时创建对象?,jsp,servlets,Jsp,Servlets,在 JSP中的代码我只想在对象y不存在时创建它。我做了一项研究,但找不到它是如何进行的 你的意思是: if(request.getSession().getAttribute("yy") == null) { usecase y = new y(); request.getSession().setAttribute("yy", y); } 可进一步简化为: if(session.getAttribute("yy") == null) { usecase y = new y();

JSP中的代码我只想在对象y不存在时创建它。我做了一项研究,但找不到它是如何进行的

你的意思是:

if(request.getSession().getAttribute("yy") == null) {
  usecase y = new y();
  request.getSession().setAttribute("yy", y);
}
可进一步简化为:

if(session.getAttribute("yy") == null) {
  usecase y = new y();
  session.setAttribute("yy", y);
}

顺便说一句,请阅读。

如果(whater==null){whater==new-TypeOfWhatever();}但如果它不是null,我仍然需要设置属性(“yy”,y);。我也需要把它修好。很抱歉,命名变量的名称不是英文的。库特鲁汉梅廷:这不是关于英语,而是关于我们以前使用的大写字母。请扩展您的问题,看起来我不明白。谢谢@plasma147回答。