【问题标题】:Connect a Swing application (client) to a servlet [closed]将 Swing 应用程序(客户端)连接到 servlet [关闭]
【发布时间】:2013-05-13 10:43:21
【问题描述】:

我必须在客户端检查我的 Swing 应用程序中的登录凭据,该应用程序由可执行 jar 文件调用。填写完详细信息后,它会签入数据库中的 servlet。我的 servlet 工作正常。

如何将 Swing 应用程序(客户端)连接到 servlet?

【问题讨论】:

标签: java swing servlets executable-jar login-control


【解决方案1】:

请看@这个帖子

How to call the Servlet from Java Swing login page using HttpClient in apache?

可能有助于解决..

干杯!!!

【讨论】:

    【解决方案2】:

    您可以使用 HttpURLConnection 向您的服务器发出 http 请求。

    例子:

    HttpURLConnection connection;
    
    
    try {
    
          String urlParameters = "username="+URLEncoder.encode(username,"UTF-8") 
                        +"&password="+URLEncoder.encode(password,"UTF-8");
          //Create connection
    
          URL url=new URL("your servlet url goes here");
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", 
               "application/x-www-form-urlencoded");
    
          connection.setRequestProperty("Content-Length", "" + 
                   Integer.toString(urlParameters.getBytes().length));
          connection.setRequestProperty("Content-Language", "en-US");  
    
          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);
    
          //Send request
          DataOutputStream wr = new DataOutputStream (
                      connection.getOutputStream ());
          wr.writeBytes (urlParameters);
          wr.flush ();
          wr.close ();
    
          //Get Response    
          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          while((line = rd.readLine()) != null) {
            // read response from your servlet
          }
          rd.close();
    
    
        } catch (Exception e) {
    
          e.printStackTrace();
    
    
        } finally {
    
          if(connection != null) {
            connection.disconnect(); 
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2012-12-05
      相关资源
      最近更新 更多