1

サーバーとサーブレットに接続されているローカルWebページに表示したい単純なアプレットがあります。

アプレットコード:

public class MyApplet extends JApplet implements ActionListener {
    JPanel panel = new JPanel();
    JButton btnPush;

    public MyApplet() {}

    public void init() {
        createGUI();
    }

    public void createGUI() {
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
        btnPush = new JButton("Push");
        btnPush.addActionListener(this);
        btnPush.setBounds(54, 94, 89, 23);
        panel.add(btnPush);
        setSize(200, 200);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnPush) {
            JOptionPane.showMessageDialog(this, "Button was pushed");
        }

    }
}

サーブレット/サーバーコードは次のとおりです。

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class MyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; chaset=utf-8");
        Writer writer = response.getWriter();

        writer.write("<applet codebase=\"bin\" code=\"MyApplet.class\" width=\"200\" height=\"200\">" + 
                "If your browser was Java-enabled, a button would appear here. </applet>");
    }

    public static void main(String... args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.addServlet(MyServlet.class, "/");

        MyApplet applet = new MyApplet();
        applet.init();
        applet.start();

        Server server = new Server(8080);
        server.setHandler(context);
        server.start();
        server.join();

    }
}

私はservlet-api3.0とjetty8を使用しています。http:// "localhost:8080"に接続できますが、アプレットをロードしようとするとロードが停止します。アプレットタグ付きのhtmlファイルを実行すると、問題なく動作します。したがって、ここではサーブレットが問題になっているようです。私は何かを忘れましたか?

4

1 に答える 1

1

サーバーの構成は、サーブレット自体にのみ応答できます。要求されている MyApplet.class ファイルを実際に返すための DefaultServlet セットアップはありません。

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);

// Serve content from bin directory (where the classes are compiled into)
ServletHolder holder = context.addServlet(DefaultServlet.class,"/*");
holder.setInitParameter("resourceBase","bin");
holder.setInitParameter("pathInfoOnly","true");

// Serve some hello world servlets
context.addServlet(MyServlet.class,"/*");

1 つのサーブレット + 1 つの DefaultServlet のより完全な例については、埋め込みの例を参照してください。 http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java? h=桟橋-8

resourceBase init パラメーターが、クラス ファイルがあるパスを指していることを確認してください。

于 2013-01-07T22:31:25.577 に答える