2014年3月29日 星期六

關於 Http request.getParameter

Q: getParameter 取得的到底是什麼東西呢?
A: Returns the value of a request parameter as a String, or null if the parameter does not exist.
Do note that it is possible to submit an empty parameter - such that the parameter exists, but has no value. For example, I could include &log=&somethingElse into the URL to enable logging, without needing to specify &log=true. In this case, the value will be an empty String ("").


若是要將取得的參數值改成數字形態:
if (request.getParameter("sum") != null &&
    request.getParameter("sum").matches("^\\d+$"))
    int sum = Integer.parseInt(request.getParameter("sum"));

沒有留言: