Re: Servlet To Java Application Communication

From: Silvio Bierman (sbierman_at_idfix.nl)
Date: 04/13/04


Date: Tue, 13 Apr 2004 15:51:42 +0200


"Anast" <anastasiosm@hotmail.com> wrote in message
news:a993bdfe.0404101939.28861f72@posting.google.com...
> Hi all,
>
> Firstly, I would like to write a Java servlet that it will be able to
> connect with a database (mySQL) and search its contents (using JDBC).
> Then, I would like to build a java application (NOT an applet), so
> that it would be able to search for queries the database, and display
> the results. Have you got any working examples that demonstrate the
> previous described functionality or any similar? I have installed
> Tomcat with MySQL Connector/J and MySQL in my box and work fine. Any
> help would be appreciable.
>
> Many thanks in advance.

In spite of other remarks there is nothing unusual or undesirable about what
you are planning to do...

Look at the URL class. It allows you to do a HTTP GET or POST which is the
way to communicate with a servlet. If you plan to do SOAP requests you
probably know you should encode the request XML in the body part of a POST
and expect the response XML in the response content.

Servlet side:
-implement doPost(request,response)
-use request.getInputStream to read the request-body.
-use request.getOutputStream to write the response.

Application(client) side:
   URL url = new URL("http://someserver:someport/somepath");
   URLConnection con = url.openConnection();
   con.setDoOutput(true);
   con.setDoInput(true);
   con.setUseCaches(false);
   OutputStream out = con.getOutputStream();
   //use out to write the request
   out.close();
   InputStream in = con.getInputStream();
   //use in to read the response
   in.close();

You can use this construct do do ny type of (HTTP or HTTPS) communication.
You can exchange strings, raw bytes, XML or even serialized Java objects
this way. Using SOAP is fine when going cross-app/cross-language, it is
somewhat bloated in other situation but if you want you can support several
protocals off course.

Consider using GZIP encoding if requests/responses tend to get big and you
might want to support narrow bandwidths.

Good luck,

Silvio Bierman



Relevant Pages

  • Re: Request for suggestions re: architecture of IntraNet/Socket app[s]
    ... Your Ajax routine could be used to call an ISAPI dll from the browser, ... Here is the Simple Java Servlet ... @param request servlet request ... @param response servlet response ...
    (borland.public.delphi.non-technical)
  • Problem with XMLHTTP responseText empty
    ... I've implemented a FileUpload servlet using AJAX and JS. ... the response in Javascript and write it out to a field on my webpage. ... // first check if the upload request coming in is a multipart ...
    (comp.lang.javascript)
  • Servlet design question
    ... for that request only (e.g. request-related stuff scooped out of ... I ended up passing request and response parameters ... containing all the session state info and stored a SessionState object ... Even so, I end up with some really huge servlet classes, with maybe ...
    (comp.lang.java.programmer)
  • Re: Do it yourself Webserver (Teil 4)
    ... Das funktioniert IMHO in Java nicht, weil ich mir nicht den Zustand des Systemstacks merken kann, was ich müsste, um das Programm genau an dieser Stelle fortsetzen zu können. ... "webRead+ webRead" eine weitere Response haben, ... Per Konvention startet eine PLT-Scheme-Webanwendung mit einer Funktion "start", die den initialen Request bekommt und eine web-response liefert soll. ... class OnePage extends Servlet { ...
    (de.comp.lang.java)
  • Re: how to send a stream to servlet doPost
    ... exceptions or HTTP error codes as well. ... It's two different system, One is java application,other is ... use java send a stringto servlet, ... When I get the response code, ...
    (comp.lang.java.programmer)