{"id":518,"date":"2017-02-14T00:00:03","date_gmt":"2017-02-13T16:00:03","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=518"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"javaweb-article55","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/14\/javaweb-article55\/","title":{"rendered":"Javaweb\u4e2d\u5c01\u88c5servlet\u5bf9post\u53c2\u6570\u7684\u5404\u79cd\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5"},"content":{"rendered":"<h2>\u5728Javaweb\u4e2d\u8868\u5355\u4e2d\u7684\u53c2\u6570\u5728\u63d0\u4ea4\u7ed9servlet\u65f6\u90fd\u662f\u4ee5\u5b57\u7b26\u4e32\u7684\u7c7b\u578b\u51fa\u73b0\uff0c\u9700\u8981\u5bf9\u5404\u79cd\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u800c\u5982\u679c\u6bcf\u6b21\u90fd\u5199\u4e00\u904d\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\uff0c\u4ee3\u7801\u7684\u5b9e\u7528\u6027\u592a\u4f4e(\u5176\u5b9e\u662f\u61d2\u5f97\u5199)\uff0c\u4e8e\u662f\u628a\u8fd9\u4e9b\u65b9\u6cd5\u5199\u5230\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4ee5\u540e\u76f4\u63a5\u8c03\u7528\u5c31\u597d\uff0c\u5176\u4e2d\u6709\u53c2\u6570\u7684\u7a7a\u503c\u5224\u65ad\uff0cInt\u7c7b\u578b\u8f6c\u6362\uff0cDouble\u7c7b\u578b\u8f6c\u6362\uff0cArray\u7c7b\u578b\u8f6c\u6362\uff0cDate\u7c7b\u578b\u7684\u8f6c\u6362\uff0cTimestamp\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f7f\u7528\u65f6\u653e\u5165util\u5305\u4e2d\u5373\u53ef\uff0c\u4e0b\u9762\u662f\u6e90\u4ee3\u7801\u3002<\/h2>\n<pre><code>package cn.itshixun;\nimport java.sql.Date;\nimport java.sql.Timestamp;\nimport javax.servlet.http.HttpServletRequest;\npublic class RequestUtil {\n    public static String getString(HttpServletRequest req, String name) {\n        String str = req.getParameter(name);\n        str = isBlank(str) ? \"\" : str.trim();\n        return str;\n    }\n    public static int getInt(HttpServletRequest req, String name) {\n        try {\n            return Integer.parseInt(RequestUtil.getString(req, name));\n        } catch (Exception ex) {\n            return 0;\n        }\n    }\n    public static int[] getIntArray(HttpServletRequest req, String name) {\n        String[] strs = req.getParameterValues(name);\n        if (strs == null) {\n            return null;\n        }\n        int[] array = new int[strs.length];\n        for (int i = 0; i &lt; strs.length; i++) {\n            array[i] = Integer.parseInt(strs[i]);\n        }\n        return array;\n    }\n    public static double getDouble(HttpServletRequest req, String name) {\n        try {\n            return Double.parseDouble(RequestUtil.getString(req, name));\n        } catch (Exception ex) {\n            return 0.0;\n        }\n    }\n    public static Date getDate(HttpServletRequest req, String name) {\n        try {\n            return Date.valueOf(RequestUtil.getString(req, name));\n        } catch (Exception ex) {\n            return null;\n        }\n    }\n    public static Timestamp getTimestamp(HttpServletRequest req, String name) {\n        try {\n            return Timestamp.valueOf(RequestUtil.getString(req, name));\n        } catch (Exception ex) {\n            return null;\n        }\n    }\n    private static boolean isBlank(String str) {\n        return str == null || str.trim().length() == 0;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Javaweb\u4e2d\u8868\u5355\u4e2d\u7684\u53c2\u6570\u5728\u63d0\u4ea4\u7ed9servlet\u65f6\u90fd\u662f\u4ee5\u5b57\u7b26\u4e32\u7684\u7c7b\u578b\u51fa\u73b0\uff0c\u9700\u8981\u5bf9\u5404\u79cd\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u800c\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/14\/javaweb-article55\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":519,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[33],"tags":[48],"class_list":["post-518","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-javaweb","tag-javaweb","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":1485,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/518\/revisions\/1485"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/519"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}