从java程序向greasemonkey应用程序发送数据?
我想在作为服务器的java程序和作为客户端的greasemonkey(java脚本应用程序)之间创建一个连接 我可以从客户端接收数据,但我应该怎么做才能将数据从服务器发送到客户端? 我在服务器中使用OutputStream将数据发送到客户端,但它似乎不起作用。在客户端,我使用下面的代码发送和接收数据:从java程序向greasemonkey应用程序发送数据?,java,javascript,network-programming,greasemonkey,Java,Javascript,Network Programming,Greasemonkey,我想在作为服务器的java程序和作为客户端的greasemonkey(java脚本应用程序)之间创建一个连接 我可以从客户端接收数据,但我应该怎么做才能将数据从服务器发送到客户端? 我在服务器中使用OutputStream将数据发送到客户端,但它似乎不起作用。在客户端,我使用下面的代码发送和接收数据: GM_xmlhttpRequest({ method: 'POST', url: "http://localhost:8888", headers: { 'Content-type' :
GM_xmlhttpRequest({
method: 'POST',
url: "http://localhost:8888",
headers: {
'Content-type' : 'application/x-www-form-urlencoded',
},
data : 'page_contents=' + window.location,
onload : function(responseDetails) {
alert('Request for Atom feed returned ' + responseDetails.status +
' ' + responseDetails.statusText + '\n\n' +
'Feed data:\n' + responseDetails.responseText);
}
});
我在服务器中使用OutputStream,但似乎它不工作或不关联任何OutputStream:(我尝试了基本通信,但它不工作,只接收数据)
如有任何建议,将不胜感激
非常感谢。当您使用Java时,我想您是在使用Servlet与服务器通信 一个有效的示例可以如下所示:
public class myServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
// for text data you could write something like this:
PrintWriter output = response.getWriter();
output.println("Hello, World\n");
// for binary data you could use the output stream this way:
// Object binary_data = new Object();
// ServletOutputStream output = response.getOutputStream();
// output.print(binary_data);
}
对于更高级的输出,我会选择使用SpringWebMVC之类的框架
支持提供JSP视图,并封装对输出流的低级访问
希望这有帮助Greasemonkey代码很好,但是您没有显示足够的Java代码来帮助这一点。感谢Java代码。问题是一个简单的套接字不能完成web响应所需的所有协议和转换。使用servlet或标准web应用程序。抱歉。当我创建帖子时,问题被编辑了。我会尽快修改我的答案。谢谢你的回复。但我不使用servlet,我只想创建一个服务器套接字并通过输入和输出流传输数据。我有简单的输入和输出。但我无法与他们沟通。@shohreh,因此出现了问题。有很多的握手和包装正在进行。你需要复制所有这些。使用servlet要聪明得多。
public class myServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
// for text data you could write something like this:
PrintWriter output = response.getWriter();
output.println("Hello, World\n");
// for binary data you could use the output stream this way:
// Object binary_data = new Object();
// ServletOutputStream output = response.getOutputStream();
// output.print(binary_data);
}