John D.
John D.

Reputation: 81

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

I wanted to get data from html file and send it to java for MongoDb updates, how do I get data?

Upvotes: 0

Views: 620

Answers (1)

Radhesh Vayeda
Radhesh Vayeda

Reputation: 901

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.

P.S.: 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.

Upvotes: 5

Related Questions