java - How can I make my website communicate with my servlet? -
i've created web app gets jqtree "coordinates" , send these coordinates var positions
server gotten servlet create file containing "coordinates", however, i don't know how going make communication. me?
thanks in advance.
i made kind of chart right below explaining idea.
1 - user changes tree
2 - positions gotten code
$(document).ready(function() { var positions; //var data dynamic json file should created in backend. var data = [{ label: 'node1', id: 1, children: [{ label: 'child1', id: 2 }, { label: 'child2', id: 3 }] }, { label: 'node2', id: 4, children: [{ label: 'child3', id: 5 }] }]; $('#tree1').tree({ data: data, autoopen: true, draganddrop: true }); console.log($('#tree1').tree('tojson')); //this give loading jqtree structure. $('#tree1').bind( 'tree.move', function(event) { event.preventdefault(); // move first, , _then_ post back. event.move_info.do_move(); console.log($(this).tree('tojson')); //this give latest tree. positions = $(this).tree('tojson'); alert(positions); $.post('http://myserver', { tree: $(this).tree('tojson') }); alert("done"); //this post json of latest tree structure. } ); });
3 - should sent ajax
$(function() { alert("file has been sent"); var data = new formdata(); data.append("custom_css", positions); $.ajax({ url: 'myserver', type: 'post', data: data, cache: false, datatype: 'json', processdata: false, contenttype: false, success: function(response) { alert("file has been sent"); }, error: function(jqxhr, textstatus, errorthrown) { alert('errors: ' + textstatus); } }); });
4 - , received servlet , saved text file: positions.txt
package com.srccodes.example; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * servlet implementation class helloworld */ @webservlet("/helloworld") public class helloworld extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public helloworld() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); printwriter printwriter = response.getwriter(); printwriter.println("<h1>hello world!</h1>"); string position = "jqtree positions"; printwriter writer = new printwriter("positions.txt", "utf-8"); writer.println(position); writer.close(); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub } }
- your .ajax url should match @webservlet() mapping. in case
"/helloworld"
- since
post
ing data, should overridedopost()
method in servlet.- finally in
dopost()
method, retrieve data"custom_css"
variable ..request.getparameter("custom_css")
- proceed rest of business logic.
$.ajax({ url: '/helloworld', type: 'post', data: data, cache: false,
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getparameter("custom_css") // proceed business logic here, using above data // // // // }
Comments
Post a Comment