2

How would you create a server-side java applet?

user364893
  • 21
  • 1
  • 2
  • What do you mean by server-side applet? What kind of functionality are you looking to implement? – Ben S Jun 11 '10 at 20:01

3 Answers3

4

If I understand correctly, you are looking for Servlets. Read the linked documentation.

Otherwise, your question doesn't make sense - the server is processing multiple requests, without any GUI, and applets are GUI.

Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
1

If you just want java code that runs on the server, you probably do want Servlets. Or perhaps JSP, if you're just looking for something to do simple processing.

Curtis
  • 3,720
  • 1
  • 16
  • 25
0

A Java applet on the client side does not necessarily require a Java webserver on the server side. Since the only communication protocol you'd like to use is HTTP which is universal, any HTTP server would suffice. You can use a "plain vanilla" webserver like Apache HTTPD with PHP. You can also use a Java Servletcontainer like Apache Tomcat which supports JSP/Servlet. You can also use a C#/.NET webserver like IIS which supports ASP. Just make use the capabilities of the webserver which you're currently already using to serve the webpage with the applet.

All you basically need to do in the Applet is to fire and process HTTP requests. You can do that with java.net.URLConnection (mini tutorial here) or with the more convenienced Apache HttpComponents Client (tutorial here). You can use Applet#getCodeBase() to obtain the context URL where the applet is served from.

URL url = new URL(getCodeBase(), "script.php"); // PHP code
// or
URL url = new URL(getCodeBase(), "servletUrl"); // Servlet code
// or
URL url = new URL(getCodeBase(), "script.asp"); // ASP code

In the server side you just return response in whatever format you like the usual way. A plain vanilla String or a more easy processable JSON or XML format. All mentioned languages provides facilities/libraries to encode/decode the data in JSON/XML formats.

As to sending parameters from Applet to the server side, just pass HTTP request parameters along as a query string in the request URL (HTTP GET) or in request body (HTTP POST). In PHP you can gather them by $_GET and $_POST and in Java Servlet by request.getParameter().

As to returning the data from the server side, in PHP you just use echo to write the response. In Java Servlet you just write to response.getWriter() and in ASP I actually have no idea, but you should got the picture now. In the Applet you should then read and process the response accordingly. See the aforementioned tutorial links how to do that.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452