Java小程序无法通过浏览器访问mysql [英] Java applet can't access mysql via browser

查看:36
本文介绍了Java小程序无法通过浏览器访问mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 小程序连接到本地主机上的 mysql 数据库以进行测试.当我从 eclipse 运行它时它工作正常,但它无法通过浏览器访问 db.我想知道是因为浏览器出于某种原因不支持 localhost db 还是我的代码有问题.如果浏览器不支持它,是否可以在不上传到服务器的情况下以某种方式对其进行测试?

My Java applet connects to mysql database on localhost for testing purposes. It works fine when I run it from eclipse, but it can't access db via browser. I wanted to know is it because browsers just don't support localhost db for some reason or I have problems with my code. And if browsers just don't support it, is it possible to test it somehow without uploading on the server ?

推荐答案

这是因为您的小程序没有访问计算机本地端口的安全权限.Java 这样做是为了保护人们的计算机.想一想您是否打开了一个网页并且 Applet 被允许访问您的本地端口.您的计算机可能会在几秒钟内被黑客入侵.

如果您想按照自己的方式进行操作,您需要将小程序打包在一个 jar 文件中,对其进行签名并验证,以便它可以接收那些权限.如果您有兴趣,这里有一些信息供您参考:

The reason why is because your applet does not have the security permissions to access local ports on the computer. Java does this to protect peoples computers. Think about if you opened up a webpage and the Applet was allowed to access your local ports. You could have your computer hacked in seconds.

If you want to do it the way you're doing it, you need to package your applet in a jar file, sign it and verify it so that it can receive those permissions. Here is some info for you if you're interested:

如何创建 jar 文件
如何对 jar 文件进行签名和验证
如何运行jar打包软件

如果这是您想要采用的方法,那么所有这些都是很好的资源.我要做的是设置一个 Servlet(参见 duffymo 的回答)或设置一个 ProxyServer 来在您的计算机和小程序之间中继信息.如果您需要更多信息,请告诉我,我可以提供.

All of those are great resources if thats the approach you want to take. What i would do is setup a Servlet (see duffymo's answer) or setup a ProxyServer to relay the information between your computer and the applet. If you want any more information let me know and i can provide it.

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

This is sort of a lot of code but i think you will get the idea, i hope this helps. You of course will have to write the client end that connects to the server and requests information from the database.

数据库服务器.java:

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:

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 内容添加到服务器,然后编写小程序的客户端部分.祝你好运!

Your applet will connect to this server, and then you can send messages to the server to request information from the database. All you have to do is add the mysql stuff to the server and then write the client portion of your applet. Good luck!

这篇关于Java小程序无法通过浏览器访问mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆