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.
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.
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.
发布评论
评论(3)
如果我理解正确的话,您正在寻找 Servlet。阅读链接的文档。
否则,你的问题没有意义 - 服务器正在处理多个请求,没有任何 GUI,而小程序是 GUI。
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.
如果您只想在服务器上运行 Java 代码,那么您可能确实需要 Servlet。或者也许是 JSP,如果您只是在寻找进行简单处理的东西。
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.
客户端的 Java applet 不一定需要服务器端的 Java Web 服务器。由于您想要使用的唯一通信协议是通用的 HTTP,因此任何 HTTP 服务器就足够了。您可以使用“普通”网络服务器,例如 Apache HTTPD 和 PHP.您还可以使用 Java Servlet 容器,例如 Apache Tomcat,它支持 JSP/Servlet。您还可以使用 C#/.NET 网络服务器,例如支持 ASP。只需利用您当前已使用的网络服务器的功能来通过小程序为网页提供服务即可。
您在 Applet 中基本上需要做的就是触发并处理 HTTP 请求。您可以使用
java.net 来完成此操作。 URLConnection
(这里有迷你教程)或者使用更方便的Apache HttpComponents Client (教程在这里)。您可以使用Applet#getCodeBase()
获取提供小程序的上下文 URL。在服务器端,您只需按照通常的方式以您喜欢的任何格式返回响应即可。普通的
String
或更容易处理的 JSON或 XML 格式。所有提到的语言都提供了对 JSON/XML 格式的数据进行编码/解码的工具/库。至于从 Applet 向服务器端发送参数,只需将 HTTP 请求参数作为请求 URL(HTTP GET)或请求正文(HTTP POST)中的查询字符串传递即可。在 PHP 中,您可以通过
$_GET
和$_POST
收集它们,在 Java Servlet 中,可以通过request.getParameter()
收集它们。至于从服务器端返回数据,在 PHP 中您只需使用
echo
来编写响应。在 Java Servlet 中,您只需写入response.getWriter()
,而在 ASP 中,我实际上不知道,但您现在应该明白了。然后,您应该在 Applet 中读取并相应地处理响应。请参阅上述教程链接如何执行此操作。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 useApplet#getCodeBase()
to obtain the context URL where the applet is served from.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 byrequest.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 toresponse.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.