问题
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