2013-09-25 63 views
1

我想使用vaadin将授权数据从小应用程序发送到服务器。如果我理解正确,我必须使用像Screenshot addon中的方法,但我不明白在其代码开发人员创建http连接的位置。 我尝试使用httpUrlconnection,但我失败并可能创建连接。将数据从小应用程序发送到vaadin上的服务器

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

如果我理解正确,您希望通过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请求的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代替,但它仍然不安全,因为请愿书可以通过“中间人”阅读,所以您必须至少对其进行加密以防止某种注射。

相关问题