0

To make a poll form using an applet, i wanted to know how can my applet communicate with a servlet . That servlet is meant to write the result to the text file on the server. I have no idea how can i do this.

saplingPro
  • 18,513
  • 51
  • 134
  • 185
  • *"I have no idea how can i do this."* What 'this'? Create an applet? Connect an applet to a web service? Implement the web service? And as an aside. Could the person that thought "This question shows research effort, it is useful & clear" explain their logic? I think that so far it meets none of those 3 criteria. – Andrew Thompson Jun 24 '12 at 07:12

3 Answers3

1

The applet and the servlet is separate. There is no easy way you can use magic to make this easier.

A servlet is a snippet inside a web server which gets executed when a HTTP request is being made to the correct URL on the web server. Hence you need to make a HTTP request to the correct URL on the web server where your servlet is running.

This is done in the same way you do any other HTTP request from an applet, which is done in the same way that you do a HTTP request from a self-standing application.

Thorbjørn Ravn Andersen
  • 68,906
  • 28
  • 171
  • 323
1

You could use java.net.URLConnection for this.

Assuming that your servlet is mapped on an URL pattern of /myservlet and your applet is been served from the context root, then this should do:

InputStream servletResponse = new URL(getCodeBase(), "myservlet").openStream();
// ...

That's all. The getCodeBase() is inherited from Applet class and dynamically returns the applet's code base URL (from where the applet was been downloaded). The servletResponse will contain whatever you wrote to response.getOutputStream() or response.getWriter() in the servlet. For example just an "ok" string or an easily parseable format like XML or JSON. You could pass request parameters as a query string in the GET request URL, or in the POST request body.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • There is something is am missing but don't know what it is.[Code that runs when i click the button on the applet to fire HTTP get request](http://i45.tinypic.com/2uh4z1l.png). [This is the code in the servlet . This is the code in the doGet method](http://i50.tinypic.com/2cr7lhz.png) Though the servlet's `doGet` method is called,but i don't see either a servlet or a jsp page in the browser. why is that ? I only see a message `inside doGet method` in the server log. what is that am i missing ? – saplingPro Jul 07 '12 at 11:37
0

Well you have several options for the applet/servlet communication....

  • http requests. (This may be the easiest). For this you can use Apache HTTP Components
  • Remote Method Invocation RMI. This may be more complicated than http requests but it depends on what you want to achieve.
  • Sockets. (I think http requests is flexible enough for your use case but just in case)
  • javascript. You can call a javascript function from your applet and let the javascript function submit the information to the servlet through ajax, websockets, etc.

Of course there are many other options but these are some ideas and remember that you may need to sign your applet.

If your question is about how to write to a file there are many tutorials. Here is a good one

Enrique
  • 9,088
  • 7
  • 43
  • 56