Introduction to Servlets (ii)

Source: Internet
Author: User

—————————— 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)

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.