【问题标题】:Query MySQL Database From Canvas Element?从 Canvas 元素查询 MySQL 数据库?
【发布时间】:2011-10-22 10:42:48
【问题描述】:

好的,所以我想尝试找出制作在浏览器中运行的简单 2D 游戏的最佳方法。我看过 HTML5 和 Canvas 元素。不过,您似乎使用了 JavaScript,而我读过您无法从 JavaScript 连接到 MySQL 数据库,因此似乎使用 JavaScript 的 Canvas 元素的连接似乎不可行。

我也一直在研究在浏览器中运行一个 Applet 并与一个 Servlet 通信,然后连接到浏览器,然后将数据库中的信息传递回 Applet。我读到这是更好的方法来避免连接来自不安全且可能允许人们访问数据库的小程序的海峡。我不太确定他们将如何注入代码并连接到我的数据库,所以如果有人能阐明这个话题,我将不胜感激。无论如何,我无法让我的 Servlet 正常工作。我正在使用 Tomcat 并在我的 Applet 中使用以下代码:

private static URLConnection getServletConnection()
        throws MalformedURLException, IOException {
    URLConnection connection;

    // Open the servlet connection
    URL urlServlet = new URL("http://localhost:8080/examples/servlets/test_servlet");
    connection = urlServlet.openConnection();

    // Config
    connection.setDoInput(true);
    connection.setDoOutput(true);

    connection.setUseCaches (false);
    connection.setDefaultUseCaches (false);

    connection.setRequestProperty(
            "Content-Type",
            "application/x-java-serialized-object");

    return connection;
}

private static void onSendData() {
    try {
        URLConnection connection;

        // get input data for sending
        String input = "Applet string heading for servlet";

        // send data to the servlet
        connection = getServletConnection();
        OutputStream outstream = connection.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outstream);
        oos.writeObject(input);
        oos.flush();
        oos.close();

        // receive result from servlet
        InputStream instr = connection.getInputStream();
        ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
        String result = (String) inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();

        // show result
        //textField.setText(result);
        System.out.println(result);

    } catch (java.net.MalformedURLException mue) {
        //textField.setText("Invalid serlvetUrl, error: " + mue.getMessage());
        System.out.println("Invalid serlvetUrl, error: " + mue.getMessage());
    } catch (java.io.IOException ioe) {
        //textField.setText("Couldn't open a URLConnection, error: " + ioe.getMessage());
        System.out.println("Couldn't open a URLConnection, error: " + ioe.getMessage());
    } catch (Exception e) {
        //textField.setText("Exception caught, error: " + e.getMessage());
        System.out.println("Exception caught, error: " + e.getMessage());
    }
}

我的 Servlet 中有以下代码:

public class test_servlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
response.setContentType("text/html;charset=ISO-8859-1");
PrintWriter out = response.getWriter();
String defect = request.getParameter("defect").toString();

try {
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet at " + request.getContextPath() + "</h1>");
    out.println("<p>Defect: " + defect + "</p>");
    out.println("</body>");
    out.println("</html>");
} finally {
    out.close();
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

try {
    response.setContentType("application/x-java-serialized-object");

    InputStream in = request.getInputStream();
    ObjectInputStream inputFromApplet = new ObjectInputStream(in);

    String servletText = "Text from Servlet";

    // echo it to the applet
    OutputStream outstr = response.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(outstr);
    oos.writeObject(servletText);
    oos.flush();
    oos.close();

} catch (Exception e) {
    e.printStackTrace();
}

processRequest(request, response);
}

当我尝试在 Eclipse 中编译 Applet 时,它给了我“无法打开 URLConnection,错误:http://localhost:8080/examples/servlets/test_servlet”响应。我认为这可能与我保存 Servlet 文件的方式/位置有关。我只是把它扔到了examples/servlet 文件夹中。这对我来说非常令人困惑,我试图弄清楚这个 AppletServlet 通信以及如何让它工作。我看过其他帖子,他们让我走到了这一步。

【问题讨论】:

  • “这一切让我很困惑” 在这种情况下混合使用 applet、servlet 和 DB 会给你带来很多麻烦。我建议作为第 1 步来弄清楚如何将 servlet 连接到数据库。只有当它起作用时,才考虑将小程序或 JS 加入其中。 (顺便说一句 - 如果小程序可以连接到 servlet,那么 JS 也可以。)
  • @AndrewThompson 嗯,所以我可以继续使用 Javascript/Canvas 并在 JS 和 servlet 之间进行通信,并允许 servlet 与 DB 进行通信并用所需的信息回复 JS?
  • 在 servlet 正常工作之前,我不建议您做任何事情

标签: java mysql servlets applet communication


【解决方案1】:
URL urlServlet = new URL("http://localhost:8080/examples/servlets/test_servlet");
connection = urlServlet.openConnection();

不要这样形成 URL。而是使用类似的东西..

URL urlServlet = new URL(getDocumentBase(), "../servlets/test_servlet");
connection = urlServlet.openConnection();

假设小程序位于与examples 目录相同的目录中。

【讨论】:

  • 顺便说一句,../ 部分是不必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多