小程序 - Servlet的通信 [英] Applet - Servlet Communication

查看:122
本文介绍了小程序 - Servlet的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经放弃了我刚才的追求,使小程序与数据库直接沟通,即使用户和网页说,这是可能的。现在我试图让我的小程序来传递信息(字符串和布尔格式)文本框输入或通过复选框指示,并给这个给servlet,然后适当地存储在数据库中。我已经得到了小应用程序前端 - 图形用户界面 - 建成,该servlet - 数据库连接还内置。唯一的问题是两个,小程序和servlet之间的联系。一个人怎么会从applet传递字符串数据到一个servlet?

I have abandoned my earlier quest to make the applet communicate directly with the database, even though users and webpages have said that it's possible. I am now trying to get my applet to pass information (String and boolean format) entered in textfields or indicated by checkboxes, and give this to the servlet, which then stores it appropriately in the database. I've got the applet front end - the GUI - built, and the servlet - database connection also built. The only problem is the link between the two, applet and servlet. How would one pass String data from an applet to a servlet?

谢谢,
约瑟夫-G。

Thanks, Joseph G.

推荐答案

首先,你得承认,你只能从那里您的小程序下载的服务器,包括端口号,除非你想沟通勾搭权限小程序签名和所有说大话。这也不仅仅是一个小程序的限制,同样适用于Flash和JavaScript(虽然在JavaScript中的情况下,有技巧,以绕过它)。

First up, you have to acknowledge that you can only communicate with the server from where your applet was downloaded from, that includes the port number, unless you want to mess around with permissions, applet signing and all that malarky. This also isn't just an Applet restriction, the same applies to Flash and JavaScript (though in the case of JavaScript there are tricks to get around it).

无论使用得到codeBase的()或getDocumentBase()你的小程序的方法将让你一个网址,从中可以得到建立,一个新的URL所需的零部件,可以让你调用一个servlet的。

Using either the "getCodeBase()" or "getDocumentBase()" method on your Applet will get you a URL from which you can get the component parts required to build a new URL that will let you call a servlet.

因此​​,您的小程序必须被从你的servlet托管在同一台服务器提供服务。

Thus, your Applet must be being served from the same server that your servlet is hosted on.

例如。如果你的Applet是在以下页面:

e.g. if your Applet is in the following page:

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

...这意味着你可以拨打电话到以开头的网址

...it means you can make calls to any URL that starts with

http://www.example.com/

...相对容易。

以下是粗糙的,未经检验的,例如展示如何调用一个Servlet。这假设code这个片断是从Applet的实例中调用。

The following is a crude, untested, example showing how to call a Servlet. This assumes that this snippet of code is being called from within an instance of Applet.

URL codeBase = getCodeBase();
URL servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "/myServlet");

// assumes protocol is http, could be 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());

然后在你的servlet,你可以通过重写的doPost(),并从请求读取输入流发送文本(没有显示异常处理和只能读取的第一行):

Then in your servlet, you can get the text sent by overriding doPost() and reading the input stream from the request (no exception handling shown and only 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);

}

当然,这只是一种方法。你也可以把你的投入和建立一个查询字符串像这样(URL编码未显示):

Of course, that's just one approach. You could also take your inputs and build up a query string like this (URLEncoding not shown):

String queryString = "inputa=" + view.getInputA() + "&inputb=" + view.getInputB();

和追加,为您的网址:

URL servletURL = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "/myServlet?" + queryString);

然而,它似乎相当普遍建立某种形式的字符串,并将其传输到该servlet,而不是这些天。

However, it seems fairly common to build up some kind of string and stream it to the servlet instead these days.

一个建议的格式是JSON,因为它是半结构化的,而且易于阅读和有很多周围的(反)序列中的小程序,并在你的servlet应该工作。这意味着你可以有你的数据一个不错的对象模型,它可以你的Applet和Servlet之间共享。建立复杂的输入的查询字符串可以是一个脑筋急转弯。

A recommended format would be JSON as it's semi-structured, while being easy to read and there are plenty of (de)serializers around that should work in your Applet and in your servlet. This means you can have a nice object model for your data which you could share between your Applet and Servlet. Building up a query string of complex inputs can be a mind bender.

同样,你可以实际使用Java序列化和流二进制到Servlet,然后使用Java序列化来创建相应的Java对象。但是,如果你坚持要像JSON,这将意味着你的servlet是更加开放的再利用,因为Java序列化从未实现的Java之外(即我所知道的)。

Likewise, you could actually use Java serialisation and stream binary to your Servlet which then uses Java serialisation to create the appropriate Java objects. However, if you stick to something like JSON, it'll mean your servlet is more open to re-use since Java serialisation has never been implemented outside of Java (that I am aware of).

这篇关于小程序 - Servlet的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆