【发布时间】:2012-02-12 06:59:50
【问题描述】:
我有一个需要向 servlet 提交分数的小程序,但它不能正常工作。
这是小程序的代码
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();
}
}
这是servlet代码
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();
}
现在我尝试通过浏览器访问 servlet,只是为了确保地址正确(它是正确的),但是由于某种原因,当我尝试从 applet 本身访问它时,它没有连接。 (调试器甚至没有启动)。
(根据建议将 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>
这里有什么我忽略的地方吗?
【问题讨论】:
-
1) 在 servlet 的第二次尝试中添加一个 catch。 2)在servlet的每个catch中放置一个
ex.printStackTrace()。 3) 小程序可信吗? -
我会照你说的做。 (但什么是“受信任的小程序”?它在浏览器上运行,但除此之外我不知道)
-
受信任的小程序是您放入 Jar 并经过数字签名的小程序,当提示您信任经过数字签名的代码时,用户单击“确定”。由于您不知道它的含义,因此它表明它不 受信任,并且它是 沙盒。在这种情况下,不要将地址硬编码为“localhost”。而是使用带有 servlet 相对路径的代码库或文档库来形成相对 URL。这也将使小程序代码在站点之间(或在开发和生产之间)“可移植”。
-
感谢您的帮助。你有什么建议我应该在哪里寻找或使用什么关键字来找到它?
-
“用什么关键字来找到这个” 到底找到什么?请不要假设您的听众是通灵者。更多信息总比更少信息好!顺便说一句 - 很好地显示更改的代码。