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

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

问题描述

我的Java小程序连接到MySQL数据库在本地主机上进行测试。它工作正常,当我从Eclipse中运行它,但它无法通过浏览器访问分贝。我想知道是不是因为浏览器只是不支持本地主机分贝出于某种原因,或者我有我的code问题。如果浏览器只是不支持它,是它可能以某种方式进行测试,而不上传的服务器上?

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的这样做是为了保护人民的计算机。想想,如果你打开​​了一个网页,小程序被允许访问本地端口。你可以让你的电脑黑客在几秒钟。搜索结果如果你想这样做,你这样做的方式,您需要打包的小程序在一个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的答案)或设置中继计算机和小程序之间的信息的访问代理服务器。如果你想要更多的信息,让我知道,我可以提供它。

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.

编辑:

这是那种很多code的,但我想你会得到的想法,我希望这有助于。当然,你将不得不写入连接到服务器和从数据库请求信息的客户端。

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.

DatabaseServer.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;
}

您applet将连接到该服务器,然后您可以发送邮件到服务器从数据库请求信息。所有你需要做的就是在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天全站免登陆