{"id":26149,"date":"2024-03-16T06:15:55","date_gmt":"2024-03-16T06:15:55","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/"},"modified":"2024-03-22T07:53:14","modified_gmt":"2024-03-22T07:53:14","slug":"how-to-use-parameters-in-the-prehandler-method-in-springmvc","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/","title":{"rendered":"How to use parameters in the prehandler method in SpringMVC?"},"content":{"rendered":"<p>In SpringMVC, preHandler is a method used as an interceptor to preprocess before handling a request. This method can take in three parameters.<\/p>\n<ol>\n<li>HttpServletRequest represents the current HTTP request object, which can be used to obtain information related to the request, such as the request URL and parameters.<\/li>\n<li>HttpServletResponse: Represents the current HTTP response object, allowing to set properties related to the response such as status code and response headers.<\/li>\n<li>Object handler: Refers to the processor handling the current request (i.e. the method in the Controller), from which some information about the processor can be obtained.<\/li>\n<\/ol>\n<p>In the preHandler method, these parameters can be used to perform operations as needed, for example:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">@Override<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">boolean<\/span> <span class=\"hljs-title function_\">preHandle<\/span><span class=\"hljs-params\">(HttpServletRequest request, HttpServletResponse response, Object handler)<\/span> <span class=\"hljs-keyword\">throws<\/span> Exception {\r\n    <span class=\"hljs-comment\">\/\/ \u83b7\u53d6\u8bf7\u6c42\u7684URL<\/span>\r\n    <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">url<\/span> <span class=\"hljs-operator\">=<\/span> request.getRequestURL().toString();\r\n    \r\n    <span class=\"hljs-comment\">\/\/ \u83b7\u53d6\u8bf7\u6c42\u7684\u53c2\u6570<\/span>\r\n    <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">param1<\/span> <span class=\"hljs-operator\">=<\/span> request.getParameter(<span class=\"hljs-string\">\"param1\"<\/span>);\r\n    <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">param2<\/span> <span class=\"hljs-operator\">=<\/span> request.getParameter(<span class=\"hljs-string\">\"param2\"<\/span>);\r\n    \r\n    <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u7684\u5b57\u7b26\u7f16\u7801<\/span>\r\n    response.setCharacterEncoding(<span class=\"hljs-string\">\"UTF-8\"<\/span>);\r\n    \r\n    <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u7684\u5185\u5bb9\u7c7b\u578b<\/span>\r\n    response.setContentType(<span class=\"hljs-string\">\"application\/json\"<\/span>);\r\n    \r\n    <span class=\"hljs-comment\">\/\/ \u6253\u5370\u5904\u7406\u5668\u7684\u4fe1\u606f<\/span>\r\n    System.out.println(<span class=\"hljs-string\">\"Handler: \"<\/span> + handler);\r\n    \r\n    <span class=\"hljs-comment\">\/\/ \u8fd4\u56detrue\u8868\u793a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684\u62e6\u622a\u5668\u548c\u5904\u7406\u5668\uff0c\u8fd4\u56defalse\u8868\u793a\u4e2d\u65ad\u8bf7\u6c42\u5904\u7406<\/span>\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">true<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>It is important to note that the return value of the preHandler method indicates whether to continue executing the following interceptors and handlers. If it returns false, the processing flow of the request will be interrupted and the subsequent interceptors and handlers will not be executed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SpringMVC, preHandler is a method used as an interceptor to preprocess before handling a request. This method can take in three parameters. HttpServletRequest represents the current HTTP request object, which can be used to obtain information related to the request, such as the request URL and parameters. HttpServletResponse: Represents the current HTTP response object, [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-26149","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use parameters in the prehandler method in SpringMVC? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use parameters in the prehandler method in SpringMVC?\" \/>\n<meta property=\"og:description\" content=\"In SpringMVC, preHandler is a method used as an interceptor to preprocess before handling a request. This method can take in three parameters. HttpServletRequest represents the current HTTP request object, which can be used to obtain information related to the request, such as the request URL and parameters. HttpServletResponse: Represents the current HTTP response object, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T06:15:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T07:53:14+00:00\" \/>\n<meta name=\"author\" content=\"Ava Mitchell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ava Mitchell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\"},\"author\":{\"name\":\"Ava Mitchell\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\"},\"headline\":\"How to use parameters in the prehandler method in SpringMVC?\",\"datePublished\":\"2024-03-16T06:15:55+00:00\",\"dateModified\":\"2024-03-22T07:53:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\"},\"wordCount\":169,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\",\"name\":\"How to use parameters in the prehandler method in SpringMVC? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T06:15:55+00:00\",\"dateModified\":\"2024-03-22T07:53:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use parameters in the prehandler method in SpringMVC?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\",\"name\":\"Ava Mitchell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"caption\":\"Ava Mitchell\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use parameters in the prehandler method in SpringMVC? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/","og_locale":"en_US","og_type":"article","og_title":"How to use parameters in the prehandler method in SpringMVC?","og_description":"In SpringMVC, preHandler is a method used as an interceptor to preprocess before handling a request. This method can take in three parameters. HttpServletRequest represents the current HTTP request object, which can be used to obtain information related to the request, such as the request URL and parameters. HttpServletResponse: Represents the current HTTP response object, [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T06:15:55+00:00","article_modified_time":"2024-03-22T07:53:14+00:00","author":"Ava Mitchell","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Ava Mitchell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/"},"author":{"name":"Ava Mitchell","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64"},"headline":"How to use parameters in the prehandler method in SpringMVC?","datePublished":"2024-03-16T06:15:55+00:00","dateModified":"2024-03-22T07:53:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/"},"wordCount":169,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/","name":"How to use parameters in the prehandler method in SpringMVC? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T06:15:55+00:00","dateModified":"2024-03-22T07:53:14+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-use-parameters-in-the-prehandler-method-in-springmvc\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use parameters in the prehandler method in SpringMVC?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64","name":"Ava Mitchell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","caption":"Ava Mitchell"},"url":"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=26149"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26149\/revisions"}],"predecessor-version":[{"id":60299,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26149\/revisions\/60299"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=26149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=26149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=26149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}