—————————— followed by the servlet introduction (i) ——————
Here are a few ways that the server gets the request parameters, first, the value of the request parameter is obtained through the GetParameter method, the second gets the name of all request parameters through Getparameternames, and returns a enumeration type. Then iterate over the value, the third is to get the value of the same parameter name through Getparametervalues, the method returns a string array, the fourth and most important one is to encapsulate the request parameter name and value into a map object by means of the Getparametermap method , return to the map < string,string[] > object, note that the value of the key is a String array (which is designed to prevent multiple values for the same parameter name), and if a beanutils library is introduced, call its populate method to encapsulate the parameters in an object , in fact, the STRUTS2 framework encapsulates values into objects through this method, and the last one obtains data through the request's getInputStream:
InputStream in =null ; int len=0 ; byte [] Buf=new byte [ 1024x768 ]; try {in =req.getinputstream (); while ((Len=in . Read (BUF))!=-1 ) {System. out . println (new String (buf, 0 , Len)); }} catch (IOException e) {e.printstacktrace (); }
However, it can only get data through a POST request and cannot get it through get. This method is seldom used and its main function is to upload files:
InputStreaminch=NULL; OutputStream out=NULL;intlen=0;byte[] buf=New byte[1024x768];Try{inch=req.getinputstream (); out=NewFileOutputStream ("Testservlet\\download"); while((len=inch. Read (BUF))!=-1){ out. Write (BUF,0, Len); } }Catch(IOException e) {E.printstacktrace (); }
Introduction to Servlets (ii)