Applet 和 Servlet 通信
我有一个小程序需要向 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,只是为了确保地址正确(确实如此),但由于某种原因,当我尝试从小程序本身,它不连接。 (调试器甚至没有启动)。
(根据建议,将 ex.printStackTrace(); 添加到每个 try catch 中,但我不知道应该在什么地方或在哪里查找它)
调用小程序的代码看起来与此类似: 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>
有什么我在这里忽略的吗?
I have an applet that needs to submit a score to a servlet and it is not working correctly.
This is the code for the applet
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();
}
}
And this is the servlet code
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();
}
Now I have tried accessing the servlet via the browser, just to make sure that the address is correct (it is), but for some reason when I try to access it from the applet itself, it does not connect. (the debugger does not even launch).
(added the ex.printStackTrace(); to each of the try catches, as per suggestion, but I have no idea what or where I'm supposed to look for this)
The code calling the applet looks similar to this:
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>
Is there something that I am overlooking here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设法让它发挥作用。
这是小程序的代码:
这是 servlet 的代码:
感谢您的帮助,请原谅我花了太长时间才回复。
I have managed to make it work.
This is the code for the applet:
This is the code for the servlet:
Thanks for your help and forgive me for taking too long to reply.