Request to obtain request headers and request data

Source: Internet
Author: User
1 package CN. itcast. request; 2 3 Import Java. io. ioexception; 4 Import Java. io. inputstream; 5 import Java. io. printwriter; 6 Import Java. lang. reflect. invocationtargetexception; 7 Import Java. util. enumeration; 8 Import Java. util. map; 9 10 Import javax. servlet. servletexception; 11 import javax. servlet. HTTP. httpservlet; 12 Import javax. servlet. HTTP. httpservletrequest; 13 Import javax. servlet. HTTP. httpservl Etresponse; 14 15 Import Org. apache. commons. beanutils. beanutils; 16 17 import CN. itcast. user; 18 19 // GET request headers and request data 20 // obtain request data (check the request data before using it) 21 public class requestdemo extends httpservlet {22 23 public void doget (httpservletrequest request, httpservletresponse response) 24 throws servletexception, ioexception {25 26 system. out. println ("-------------- obtain method 1 ----------------"); 27 string value = reques T. getparameter ("username"); 28 If (value! = NULL &&! Value. trim (). equals ("") {29 system. out. println (value); 30} 31 32 system. out. println ("-------------- obtain method 2 ----------------"); 33 enumeration E = request. getparameternames (); 34 while (E. hasmoreelements () {35 string name = (string) E. nextelement (); 36 value = request. getparameter (name); 37 system. out. println (name + "=" + value); 38} 39 40 system. out. println ("-------------- obtain method 3 ----------------"); 41 string [] Values = request. getparametervalues ("username"); 42 for (INT I = 0; values! = NULL & I <values. length; I ++) {43 system. out. println (values); 44} 45 46 system. out. println ("-------------- obtain Method 4 ----------------"); 47 map = request. getparametermap (); 48 user = new user (); 49 50 try {51 beanutils. populate (user, MAP); 52} catch (exception E1) {53 e1.printstacktrace (); 54} 55 system. out. println (MAP); 56 57} 58 59 // method for getting the header 60 private void test1 (httpservletrequest request) throws ioexception {61 string headvalue = request. getheader ("Accept-encoding"); 62 system. out. println (headvalue); 63 system. out. println ("-------------------------------"); 64 65 enumeration E = request. getheaders ("Accept-encoding"); 66 while (E. hasmoreelements () {67 string value = (string) E. nextelement (); 68 system. out. println (value); 69} 70 71 system. out. println ("-------------------------------"); 72 E = request. getheadernames (); 73 while (E. hasmoreelements () {74 string name = (string) E. nextelement (); 75 string value = request. getheader (name); 76 system. out. println (name + "=" + value); 77 78} 79 80 system. out. println ("-------------- obtain Method 5 ----------------"); 81 82 inputstream in = request. getinputstream (); 83 int Len = 0; 84 byte buffer [] = new byte [1024]; 85 while (LEN = in. read (buffer)> 0) {86 system. out. println (LEN); 87 system. out. println (new string (buffer, 0, Len); 88} 89} 90 91 Public void dopost (httpservletrequest request, httpservletresponse response) 92 throws servletexception, ioexception {93 94} 95 96}
View code
 1 package cn.itcast; 2  3 public class User { 4     private String username[]; 5     private String password; 6  7     public String[] getUsername() { 8         return username; 9     }10 11     public void setUsername(String[] username) {12         this.username = username;13     }14 15     public String getPassword() {16         return password;17     }18 19     public void setPassword(String password) {20         this.password = password;21     }22 23 }
View code
1 <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> 2 <HTML> 3 View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.