我想使用vaadin从applet向服务器发送授权数据。如果我理解正确,我必须使用Screenshot addon中的方法,但我不明白其代码开发人员在哪里创建http连接。
我尝试使用httpUrlconnection
,但我失败了,可能是创建连接。
Glassfish说:
SEVERE: java.io.FileNotFoundException: http://localhost:8080/CheckResponse/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1624)
代码:
connection = (HttpURLConnection) new URL("http://localhost:8080/CheckResponse/").openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
我做错了什么?
答案 0 :(得分:0)
如果我理解正确,您希望通过POST-GET方法将数据与身份验证凭据(或有效身份验证)通过Java applet传递给vaadin Web应用程序。
我还没有托管小程序,但是如果你想从java中做到这一点(我认为你需要的唯一额外的东西是在你的构建路径或lib WEB-INF目录中包含apache-commons库让它工作)您必须从Java applet到您的vaadin Web应用程序执行HTTP GET / POST请求。给出的例子:
/* FROM JAVA APPLET EXECUTE JSON GET */
public static String getJson(String parameter) throws ClientProtocolException, IOException {
String url = "http://somedomain.com/VaadinApp?parameter=";
String resp="";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url + parameter); // here you put the entire URL with the GET parameters
HttpResponse response = client.execute(request);
// from here is postprocessing to handle the response, in my case JSON
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
String line = "";
StringBuilder sb = new StringBuilder("");
while ((line = rd.readLine()) != null) {
sb.append(line);
}
// if defined, this will return the response of the vaadin application, for this example no response is given from the server
return sb.toString();
}
然后,在您的vaadin应用程序中,您可以通过Vaadin Request处理GET / POST参数,在这种情况下,可以从主UI处理:
public class VaadinApp extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = VaadinApp.class)
public static class Servlet extends VaadinServlet {
}
VistaPrincipal vp;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
VistaPrincipal vp = null;
vp = new VistaPrincipal();
if(request.getParameter("parameter") != null) {
Notification.show("Success");
// show the parameter in GET request
System.out.println("parameter:" + request.getParameter("parameter"));
}
layout.addComponent(vp);
layout.setComponentAlignment(vp, Alignment.TOP_CENTER);
layout.setMargin(false);
setContent(layout);
}
}
HttpRequest可以通过多种方式进行,但在通过GET发送参数时要小心,因为它们显示在URL中。您可以使用POST,但它仍然是不安全的,因为请愿书可以由中间的人员阅读,因此您必须至少加密它以防止某种注入。