2012-01-07 6 views
0

ユーザーからの入力文字列を正常に受信し、サーバー側で処理し、その結果をWebページに表示するアプリケーションがあります。私はこれをRemoteServiceServletとして実装しました。この方法ですべてのウェブサイトガジェットを簡単に処理できるからです。RemoteServiceServletでコンテンツ処理アタッチメントを使用する

ウェブページに結果を表示する代わりに、ユーザーが処理された文字列をtxtファイルにダウンロードできるように、「コンテンツ処理添付ファイル」の可能性を使用することにしました。

アプリケーション全体をRemoteServiceServletからHttpServletに変更せずにこれを行う方法はありますか?

以下、私のコードです。感謝万円。

ProjectServiceImpl.java

public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService 
{ 
    public String project(String input) throws IllegalArgumentException 
    { 
     String output = processString(input); 
     // Below something I tried to do, but it does not work at all 
     try { 
      HttpServletResponse resp = getThreadLocalResponse(); 
     resp.reset(); 
     resp.setContentType("application/octet-stream"); 
     resp.setContentLength(10); 
     resp.setHeader("Content-disposition", "attachment; filename=\"test.txt\""); 
     ServletOutputStream op = resp.getOutputStream(); 
     op.write(convertToByteArray(output),0,10); 
     op.flush(); 
     op.close(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return output; 
    } 
} 

ProjectService.java

public interface ProjectService extends RemoteService { 
    String project(String name) throws IllegalArgumentException; 
} 

ProjectServiceAsync.java

public interface ProjectServiceAsync { 
    void project(String input, AsyncCallback<String> callback) 
      throws IllegalArgumentException; 
} 

MyProject.java:クライアント側

[...] 
projectService.project(originalString, new AsyncCallback<String>() { 
    [...] 
    public void onSuccess(final String result) 
    { 
     [...] // Or perhaps should I create here in client-side the txt file with "result" 
    } 
}); 

答えて

1

代わりに別のサーブレットへの変更の、それぞれのいずれかを使用することを検討 - RPCは、トランスポートとして使用するXMLHttpRequestがファイルをダウンロードするために使用されるが、非常にまだあることはできませんサーバーへのほとんどすべてのリクエストに対して便利です。 XHRは、javascriptからサーバーへの通信にのみ適しており、ダウンロード(またはコンテンツ付きの新しいウィンドウを開くなどの他のもの)には使用できません。

別のサーブレットを作成し、RPC呼び出しで文字列、そのサーブレットのURL(さらに何をダウンロードするかを示す他のパラメータ)を返すことを検討してください。

関連する問題