Java applet无法通过浏览器访问mysql

时间:2012-06-04 00:09:46

标签: java mysql applet

我的Java小程序连接到localhost上的mysql数据库以进行测试。当我从eclipse运行它时,它工作正常,但它不能通过浏览器访问数据库。我想知道是因为浏览器由于某种原因不支持localhost db,或者我的代码有问题。如果浏览器不支持它,是否可以以某种方式测试它而无需在服务器上上传?

2 个答案:

答案 0 :(得分:1)

原因是您的applet没有访问计算机上本地端口的安全权限。 Java这样做是为了保护人们的计算机。想想你是否打开了一个网页,并允许Applet访问你的本地端口。您可以在几秒钟内将计算机入侵。

如果您想按照自己的方式进行操作,则需要将applet打包到jar文件中,对其进行签名并对其进行验证以便可以获得这些权限。如果您有兴趣,可以在这里找到一些信息:

How to create a jar file
How to sign and verify a jar file
How to run jar packaged software

如果你想采取的方法,所有这些都是很好的资源。我要做的是设置一个Servlet(请参阅duffymo的答案)或设置一个ProxyServer来在您的计算机和applet之间传递信息。如果您想了解更多信息,请告诉我,我可以提供。


编辑:

这是很多代码,但我认为你会得到这个想法,我希望这会有所帮助。您当然必须编写连接到服务器的客户端并从数据库请求信息。

DatabaseServer.java:

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;

public class DatabaseServer implements Runnable
{
    public DatabaseServer()
    {
        System.out.println("Created new server!");
        clients = new ArrayList<Client>(); // this is a list of all the clients connected to the server
        connected = false;
        running = false;
        connect(); // starts server
    }

    public void run()
    {
        System.out.println("Waiting for clients.../n");

        while(running)
        {
            try 
            {
                Socket socket = server.accept(); // waits for a client to connect
                System.out.println("Client connected! "+socket.getInetAddress());
                Client client = new Client(this, socket); // creates a new client object
                clients.add(client);// adds it to the list


                // cleans the client list everytime a new client connects
                Iterator<Client> it = clients.iterator();
                while(it.hasNext())
                {
                    Client next = it.next();
                    if(!next.connected)
                    {
                        next.disconnect();
                        it.remove();
                    }
                }
            }catch(IOException e){e.printStackTrace();}
        }
    }

    public void connect()
    {
        if(!connected)
        {
            System.out.println("starting server...");
            try
            {
                System.out.println("opening port...");
                server = new ServerSocket(8080); // opens a server socket
                System.out.println("server started on port: "+server.getLocalPort());

                running = true;
                connected = true;

                thread = new Thread(this);// starts the server thread
                thread.start();
            }catch(Exception e){e.printStackTrace(); connected = false; running = false;}
        }
    }

    public void disconnect()
    {
        //stops the server
        try
        {
            server.close();
        }catch(Exception e){e.printStackTrace();}
        server = null;

        if(thread != null)
            thread.interrupt();
        thread = null;

        connected = false;
        running = false;
    }

    public void handleMessage(Client client, String message)
    {
        /* this is where you do your database interactions, based on the message you can pull out specific
         * information from your database and then send it back to the client using client.sendMessage()
        */
    }

    private Thread thread;
    private boolean running;
    private boolean connected;
    private ServerSocket server;
    private ArrayList<Client> clients;

    public static void main(String args[])
    {
        new DatabaseServer(); // makes a new server
    }
}

Client.java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client implements Runnable
{
    public Client(DatabaseServer server, Socket socket)
    {
        this.socket = socket;
        this.server = server;
        try
        {
            connected = true;

            writer = new PrintWriter(socket.getOutputStream()); // opens an output stream
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // opens an input stream

            running = true;
            thread = new Thread(this);
            thread.start();// starts the client thread
        }catch(Exception e){e.printStackTrace(); connected = false;}
    }

    public void run()
    {
        try 
        {
            String message = "";
            while((message = reader.readLine()) != null & running & connected) // waits for a message to be recieved
            {
                server.handleMessage(this, message); // tells server to handle message
            }
        }catch(IOException e){e.printStackTrace(); connected = false;}
    }

    public void disconnect()
    {
        // disconnects client
        try
        {
            socket.close();
        }catch(Exception e){e.printStackTrace();}

        try
        {
            reader.close();
        }catch(Exception e){e.printStackTrace();}

        try
        {
            writer.close();
        }catch(Exception e){e.printStackTrace();}

        socket = null;
        reader = null;
        writer = null;

        if(thread != null)
            thread.interrupt();
        thread = null;

        connected = false;
        running = false;
    }

    public void sendMessage(String message)
    {
        // sends a message back to the client
        writer.println(message);
        writer.flush();
    }

    public boolean connected;

    private boolean running;
    private Thread thread;
    private DatabaseServer server;
    private Socket socket;
    private PrintWriter writer;
    private BufferedReader reader;
}

您的小程序将连接到此服务器,然后您可以向服务器发送消息以从数据库请求信息。您所要做的就是将mysql内容添加到服务器,然后编写applet的客户端部分。祝你好运!

答案 1 :(得分:0)

我建议在applet和数据库之间放一个servlet。您不应该像这样将数据库暴露给Web。最好让applet向servlet发送请求并让它代表它进行调解。 servlet可以对输入进行身份验证和授权,验证和绑定,并对响应进行编组以进行回送。