How to handle the Exceptions in JSP, when session expires?

你。 提交于 2019-12-14 03:33:56

问题


I'm working on struts1.3.8. The JSP page contains scriptlet to iterate the data in the session. Once the user opens the page and not performing any operation till the session expires and then next refreshing it is throwing java.lang.NullPointerException.

So how to handle that exception and how to make the session alive?


回答1:


You can rely on the existence of a well-known attribute in session.

Set this when the session is first created.

session.setAttribute("well-known-attribute", "abcd");

In your JSP, check if this attribute exists before you do any iterations.

if(session.getAttribute("well-known-attribute") != null) {
    // iterate others now
} else {
    session.setAttribute("well-known-attribute", "abcd");
    // now add the other attributes.
}

A new session will always be created if

  • There isn't one already AND
  • There is a request associated.


来源:https://stackoverflow.com/questions/10499683/how-to-handle-the-exceptions-in-jsp-when-session-expires

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!