In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge points about the method of obtaining form data by Request in Javaweb. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
The form code login learns to play basketball and sleep request.getParamter (String name); get the value through name
Code snippet
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the value String name = request.getParameter ("username") through the name attribute; System.out.println ("obtained name is:" + name); String pwd = request.getParameter ("password"); System.out.println ("obtained password is:" + pwd);}
Running result
Request.getParamterValues (String name); get the value through name (usually used to get the value of the check box) code snippet protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the value through the name attribute String [] names = request.getParameterValues ("username"); for (String name: names) {System.out.println ("obtained name is" + name ") } String [] hobbys = request.getParameterValues ("hobby"); for (String hobby: hobbys) {System.out.println ("check box value is:" + hobby);}}
Running result
Summary: although this method is to obtain the value value through name, but for the ordinary input box is directly obtained is their input value, similar to the first method, the ordinary input box to obtain the value can be used but not recommended, generally used to obtain the value of the check box
Request.getParameterNames (); directly gets the name values of all objects in the form, and the return value is the enumeration collection
Code snippet
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the value Enumeration names2 = request.getParameterNames () through the name attribute; while (names2.hasMoreElements ()) {String name = names2.nextElement (); System.out.println (name);}}
Running result
Summary: after getting the name value, use the first method to get the real value.
Request.getParameterMap (); get the name values and data of all objects in the form directly
The return value of this method is the map collection, the collection key is of type String, and value is an array of type Sting.
Code snippet
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / represents all values through map, key in the collection represents form name value value represents form data Map map = request.getParameterMap (); / / traverses collection Set keySet = map.keySet () through keySet; for (String key: keySet) {System.out.println ("form name value:" + key) / / get all the value values by key String [] values = map.get (key); for (String value: values) {System.out.println ("data in the form:" + value);}
Running result
These are all the contents of this article entitled "what are the ways for Request to get form data in Javaweb?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.