2012-02-12 2 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) 서블릿의 각 캐치에'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) { 
    } 
} 

감사하고 답장을 너무 오래 걸리는 용서해

은 애플릿의 코드입니다.