2012-02-12 11 views
0

サーブレットにスコアを提出する必要があるアプレットがあり、正しく動作しません。 アプレットとサーブレットの通信

この

は、アプレット

private URLConnection getConnection() throws MalformedURLException, IOException { 
     URL serverAddress = null; 
     URLConnection conn = null; 
     serverAddress = new URL("http://localhost/GamesPortal/submitScore"); 
     conn = serverAddress.openConnection(); 
     conn.setDoOutput(true); 
     conn.setRequestProperty("Content-Type", "application/x-java-serialized-object"); 
     return conn; 
    } 

    private void sendRecievedata(GameInfo info) { 
     try { 
      URLConnection c = this.getConnection(); 
      OutputStream os = c.getOutputStream(); 
      ObjectOutputStream oos = new ObjectOutputStream(os); 
      oos.writeObject(info); 
      oos.close(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

ためのコードであり、これは私だけのアドレスであることを確認するために、ブラウザを介してサーブレットにアクセスしようとしている今、サーブレットコード

try { 
     HttpSession s = request.getSession(true); 

     response.setContentType("application/x-java-serialized-object"); 
     InputStream in = request.getInputStream(); 
     ObjectInputStream ois = new ObjectInputStream(in); 
     GameInfo info = (GameInfo) ois.readObject(); 

     if (info.getUserId() > 0) { 
      Scores score = new Scores(); 
      score.submitScore(info); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
    } 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try { 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet submitScore</title>");    
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h1>Servlet submitScore at " + request.getContextPath() + "</h1>"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } catch { 
     ex.printStackTrace(); 
    } finally {    
     out.close(); 
    } 

です正しいのですが、何らかの理由でアプレット自体からアクセスしようとすると接続されません。 (デバッガは起動しません)。

にアプレットを呼び出すコードを、

は(提案に従って、トライキャッチのそれぞれに、私は何か私はこれを探しになってる分からない)(ex.printStackTraceを追加しました)このようになります。 http://roseindia.net/jsp/simple-jsp-example/applet-in-jsp.shtml

<jsp:plugin code="Pong.class" name="Games/Pong/Pong" type="applet" width="800" height="600"> 
    <jsp:params> 
     <jsp:param name="userId" value="<%= user.getUserId()%>" ></jsp:param> 
    </jsp:params> 
</jsp:plugin> 

は、私がここに見下ろす午前何かはありますか?

+0

1)サーブレットの2番目のtryにキャッチを追加します。 2)サーブレットの各キャッチに 'ex.printStackTrace()'を入れます。 3)アプレットは信頼できますか? –

+0

お願いします。 (ただし、「信頼できるアプレット」とは何ですか?ブラウザ上で実行されますが、それ以外はわかりません) –

+0

信頼できるアプレットはJarに入れてデジタル署名したもので、デジタル署名されたコードを信頼するように促されます。あなたはそれが何を意味するのか分からないので、それは*信頼されておらず、それがサンドボックスであることを示唆しています。その場合、アドレスを 'localhost'にハードコードしないでください。代わりに、サーブレットへの相対パスでコードベースまたはドキュメントベースのいずれかを使用して相対URLを作成します。これにより、サイト間(または開発と生産の間)にアプレットコードが「移植可能」になります。 –

答えて

1

私はそれを動作させることができました。

これは、アプレットのコードです:

private URLConnection getServletConnection() 
     throws MalformedURLException, IOException { 
    URL urlServlet = new URL("http://localhost:8080/GamePortal/submitScore"); 
    URLConnection con = urlServlet.openConnection(); 
    con.setDoOutput(true); 
    con.setRequestProperty(
      "Content-Type", 
      "application/x-java-serialized-object"); 
    return con; 

} 

private void onSendData(GameInfo info) { 

    try { 
     // send data to the servlet 
     URLConnection con = getServletConnection(); 
     OutputStream outstream = con.getOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(outstream); 
     oos.writeObject(info); 
     oos.flush(); 
     oos.close(); 
     // receive result from servlet 
     InputStream instr = con.getInputStream(); 
     ObjectInputStream inputFromServlet = new ObjectInputStream(instr); 
     String result = (String) inputFromServlet.readObject(); 
     //JOptionPane.showMessageDialog(null, result); 
     inputFromServlet.close(); 
     instr.close(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

これは、サーブレットのコードです:あなたの助けのための

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException {   
    try { 
     response.setContentType("application/x-java-serialized-object"); 
     // read a String-object from applet 
     // instead of a String-object, you can transmit any object, which 
     // is known to the servlet and to the applet 
     InputStream in = request.getInputStream(); 
     ObjectInputStream inputFromApplet = new ObjectInputStream(in); 
     GameInfo score = (GameInfo) inputFromApplet.readObject(); 
     System.out.println(score.getScore()); 

     GameInfo info = score; 

     if (info.getUserId() > 0) { 
      Scores instance = new Scores(); 
      instance.submitScore(info); 
     } 

     OutputStream outstr = response.getOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(outstr); 
     oos.writeObject("reply"); 
     oos.flush(); 
     oos.close(); 
    } catch (ClassNotFoundException ex) { 
    } 
} 

おかげで、返信に時間がかかり過ぎるために私を許して。

関連する問題