How to get boolean value set as an attribute value in request.
Consider the following snippet
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
boolean isOriginal = (boolean) req.getAttribute(“isOriginalFile");
//Some code
}
Where the request may/may not contain the isOriginalFile
attribute. How to handle this?
Assuming that getting false
when attribute is null
is what you expect:
boolean isOriginal = Boolean.TRUE == req.getAttribute("isOriginalFile");
Then if you set the attribute to anything else than Boolean.TRUE
(including null
) you will get false
.
You may set it in either way:
req.setAttribute("isOriginalFile", Boolean.TRUE);
req.setAttribute("isOriginalFile", (Boolean) true);
req.setAttribute("isOriginalFile", true);
But not as String (because it will be then evaluated to false
):
req.setAttribute("isOriginalFile", "true");