java 项目减少魔法值,不相容的魔法值1013478509"当与服务器和QUOT推出的Java小程序失败;...

I'm having a problem with my applet. I have a school project where I'm supposed to make a pong-online game. It runs fine offline but when I try to load it from a server I just get an empty frame with a red text in it. when I click the text I get the message:

incompatible magic value 1013478509

I'm using jetty-all-8.1.8.v20121106.jar, and servlet-api-3.0.jar

The class that starts up the server looks like this:

public class TheServer extends HttpServlet {

private static final long serialVersionUID = 1L;

private Scanner sc;

private String webSite;

private PrintWriter out;

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.setContentType("text/html; charset=UTF-8");

resp.setStatus(HttpServletResponse.SC_OK);

out = resp.getWriter();

sc = new Scanner(new File("F:\\Users\\Johan\\Workspace Kurs 5\\PongOnline\\bin\\pong.html"));

webSite = "";

while(sc.hasNext())

webSite += sc.nextLine();

sc.close();

out.println(webSite);

System.out.println(webSite);

}

public static void main(String...args) throws Exception {

ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS);

context.addServlet(TheServer.class, "/");

Server server = new Server(666);

server.setHandler(context);

server.start();

server.join();

}

}

解决方案

The magic value of a valid Java class is 0xCAFEBABE (the hex value of 3405691582), which is the first 4 bytes. But you're getting 0x3C68746D (the hex value of 1013478509) which in turn stands for the ASCII characters

int magic = 1013478509;

ByteBuffer b = ByteBuffer.allocate(4);

b.putInt(magic);

System.out.println(new String(b.array()));

This in combination with the applet being served by a website suggests that it's the start of a tag which in turn suggests that it's a HTML document.

So, the HTTP request to applet has apparently actually returned a HTML document. You should be able to see it yourself when you change the current request URI in browser address bar to point to applet's URL. Then you'll see what the browser actually retrieved when it tried to download the applet. Perhaps it's a simple HTTP 404 error document in flavor of a HTML page.

To fix it, just make sure that the URL in the or tag is correct. It's relative to the current request URL as you see in browser address bar. The way how your servlet works is quite strange. You're streaming a HTML file from outside the deploy folder. This suggests that the applet is also outside the deploy folder and thus not reachable by a valid URL at all. You should put both the HTML page and the applet in the web content folder. This way you don't need that servlet anymore.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值