1 / 6

Posting Form Data

Pp 167..., Corejava. Posting Form Data. Sending Info from Browser to Server. When user clicks Submit button, text in text fields and clickbox settings, etc, are sent back to web server Web server calls a program that processes user input Many techs enable web servers to call programs

brinly
Télécharger la présentation

Posting Form Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pp 167..., Corejava Posting Form Data

  2. Sending Info from Browser to Server • When user clicks Submit button, text in text fields and clickbox settings, etc, are sent back to web server • Web server calls a program that processes user input • Many techs enable web servers to call programs • Java servlets • JavaServer Faces • Microsoft ASP • CGI scripts in Perl, C++, or whatever language • Will call them all “scripts”!!! -- not nice!!

  3. Commands (“methods”) to send info back to server • We won't discuss server-side scripts but just how to send info to server • Two commands commonly used are POST and GET • GET is easier, simply attach params to the end of the URL • ttp://host/script?parameters

  4. example • For example, Yahoo has a script, py/maps.py, at the host maps.yahoo.com • Scripts requires 2 params, addr and csz • You separate the params by an & (ampersand) and encode the parameters using URL Encoding • Replace all spaces with a + • Replace all nonalphanumeric chars by a % followed by a 2-digit hex number (ASCII code) • S. Main is S%2e+Main • Because hex 2e is the period in ASCII code • Try requesting http://maps.yahoo.com/py/maps.py?addr=1+Infinite+Loop&csz=Cupertino+CA

  5. POST command • GET is easy but often limited (visible, short) • In POST, you don't attach params to a URL • Instead, you get an output stream from the URLConnection • Then write name/value pairs to the output stream • Still have to URL-encode the values and separate them with & character

  6. Generic Example of POST URL url = new URL(“ttp://host/script”); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //default is input from server to browser PrintWriter out = new PrintWriter(connection.getOutputStream()); //PrintWriter good for sending text to server out.print(name1 + “=” + URLEncoder.encode(value1, “UTF-8”) + “&”); out.print(name2 + “=” + URLEncoder.encode(value2, “UTF-8”)); out.close(); Then, call getInputStream and read the server resposne. See the PostTest example.

More Related