"Java" sends a POST request to a page and obtains its processed results with security submitted after creating a form with JavaScript

Source: Internet
Author: User

This article compares the article "Java" reading the page (click the Open link) to send a GET request to a page and get the result after its processing, where a POST request is sent to a page and the result is obtained after its processing. If you send a GET request to a page and get the result after its processing, just read a Web page with it? After the various parameters connected with the & parameter, and send a POST request to a page, and get its processing results, it is not so simple.

This method may not be common in common Java files, but it is very common in javaweb Web programming such as JSP, Android, etc.

Import Java.io.*;import java.net.*;p ublic class Httprequestpost {/** * @param URL * requires the URL of the corresponding POST request * @param par The AM * Request parameter should be in the form of param1=value1&param2=value2. * @return The response result of the remote resource represented by */public static string sendpost (string url, string param) {printwriter out = null; BufferedReader in = null; String result = ""; try {//Open link and configure, this is the specified action URLConnection conn = new URL (URL). OpenConnection (); Conn.setrequestproperty (" Accept "," */* "); Conn.setrequestproperty (" Connection "," keep-alive "); Conn.setrequestproperty (" User-agent "," mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1); Conn.setdooutput (True), conn.setdoinput (TRUE);//Gets the output stream corresponding to the URLConnection object out = new PrintWriter ( Conn.getoutputstream ());//Send request parameter out.print (param);//output stream buffer Out.flush ();//define BufferedReader input stream to read post response after data in = New BufferedReader (New InputStreamReader (Conn.getinputstream ())); String line = null;while (line = In.readline ()) = null) {result + = line;} People walk with Doors out.close (); In.close ();} catch (Exception e) {System.OUT.PRINTLN ("Send POST request exception!") "+ e);} return result;} public static void Main (string[] args) {//If http://localhost:8080/a.jsp is a response page for a POST request//You want it to get param=1&param=2 results, Then call System.out.println (Httprequestpost.sendpost ("http://localhost:8080/a.jsp", "param=1&param=2"));}}

This shows that using Java programming makes it easy to send a POST request to a page and get the results after its processing. In Web programming, if we want the JSP to submit data to JavaScript and then submit the data to the JSP for interaction, you should not use JavaScript to create a form form, and then write the JSP data to this form and submit the method:

function Makeform () {    var formtojsp = document.createelement ("form");    Formtojsp.id = "formtojsp";    Formtojsp.name = "formtojsp";    Document.body.appendChild (formtojsp);    Create a hidden field and write the value of the JSP to this hidden field    var input = document.createelement ("input");    Input.type = "hidden";    Input.name = "value1";    Input.value = "<%=JspValue%>";    Formtojsp.appendchild (input);    Formtojsp.method = "POST";    Formtojsp.action = "/servlet_address";    Formtojsp.submit ();}
Because the ulterior motives of the browser can see your script in the source code, and then write Java program as above, to your "hidden form" response address to send the post content, so as to achieve a certain purpose. The right way to do this is to use the Request.getsession (). SetAttribute ("Jspvaluename", Jspvalue), which takes the values that need to be used by multiple servlets. This one statement is stored in the session, If required, use String Jspvalue=request.getsession (). getattribute ("Jspvaluename"). ToString (); To remove the jspvalue.

"Java" sends a POST request to a page and obtains its processed results with security submitted after creating a form with JavaScript

Related Article

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.