简体   繁体   中英

How to get data from html to java in web application?

我想从html文件中获取数据并将其发送到java以进行MongoDb更新,如何获取数据?

If the html page belongs to you can get data through ajax call or even through page submit.

But, if its third party page you need to do http get call to read content in java file. Then get element by using " jsoup " package.

PS: Your question is still not clear to me. Please give the appropriate description so that I can help accordingly.

Update

//In your html file
    <html>
    <script type= "text/javascript">
    var myData = {"myId" : 123456};
    $.ajax({
            url: 'myServletName',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(myData),
            success: function (data) {

            }

        });
    </script>
    </html>



    //In Java File (myServlet.java)


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import org.json.JSONObject;

    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
                String jsonR = "";
                if (br != null) {
                    jsonR = br.readLine();
                }
                JSONObject data = new JSONObject(jsonR);
                Long myId = data.getLong("myId");
//You can use this variable (myId) for your further operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM