简体   繁体   中英

Is there a better way to check for null in HttpServletRequest.getParamter()

Can there a better/optimized way to extract value from request ?

void m() {
    String tFlag = request.getParameter("tFlag");

    tFlag = (tFlag == null) ? "" : tFlag;
}

If you could use Java-8, you can use Optional.ofNullable as :

String tFlag = Optional.ofNullable(request.getParameter("tFlag")).orElse("");

or alternatively the suggestion from ernest_k in comments :

String tFlag = request.getParameterMap().getOrDefault("tFlag", "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM