java - Applet - Servlet Communication -


i have abandoned earlier quest make applet communicate directly database, though users , webpages have said it's possible. trying applet pass information (string , boolean format) entered in textfields or indicated checkboxes, , give servlet, stores appropriately in database. i've got applet front end - gui - built, , servlet - database connection built. problem link between two, applet , servlet. how 1 pass string data applet servlet?

thanks, joseph g.

first up, have acknowledge can communicate server applet downloaded from, includes port number, unless want mess around permissions, applet signing , malarky. isn't applet restriction, same applies flash , javascript (though in case of javascript there tricks around it).

using either "getcodebase()" or "getdocumentbase()" method on applet url can component parts required build new url let call servlet.

thus, applet must being served same server servlet hosted on.

e.g. if applet in following page:

http://www.example.com/myapplet.html

...it means can make calls url starts

http://www.example.com/

...relatively easily.

the following crude, untested, example showing how call servlet. assumes snippet of code being called within instance of applet.

url codebase = getcodebase(); url servleturl = new url(codebase.getprotocol(), codebase.gethost(), codebase.getport(), "/myservlet");  // assumes protocol http, https httpurlconnection conn = (httpurlconnection)servleturl.openconnection(); conn.setdooutput(true); conn.setrequestmethod("post");  printwriter out = new printwriter(conn.openoutputstream()); out.println("hello world"); out.close();  system.out.println(conn.getresponsecode()); 

then in servlet, can text sent overriding dopost() , reading input stream request (no exception handling shown , reads first line of input):

public void dopost(httpservletrequest req, httpservletresponse res) {     bufferedreader reader = req.getreader();    string line = reader.readline();    system.out.println("servlet received text: " + line);  } 

of course, that's 1 approach. take inputs , build query string (urlencoding not shown):

string querystring = "inputa=" + view.getinputa() + "&inputb=" + view.getinputb(); 

and append url:

url servleturl = new url(codebase.getprotocol(), codebase.gethost(), codebase.getport(), "/myservlet?" + querystring); 

however, seems common build kind of string , stream servlet instead these days.

a recommended format json it's semi-structured, while being easy read , there plenty of (de)serializers around should work in applet , in servlet. means can have nice object model data share between applet , servlet. building query string of complex inputs can mind bender.

likewise, use java serialisation , stream binary servlet uses java serialisation create appropriate java objects. however, if stick json, it'll mean servlet more open re-use since java serialisation has never been implemented outside of java (that aware of).


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -