从Canvas元素查询MySQL数据库?

时间:2011-10-22 10:42:49

标签: java mysql servlets applet communication

好的,所以我想找出制作一个在浏览器中运行的简单2D游戏的最佳方法。我看过HTML5和Canvas元素。虽然你似乎使用了JavaScript而且我已经读过你无法从JavaScript连接到MySQL数据库,所以从Canvas元素(似乎使用JavaScript)的连接似乎不合适。

我一直在寻找在浏览器中运行Applet并与Servlet进行通信,然后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文件夹中。这对我来说非常混乱,我正在试图找出这个Applet&lt; - &gt; Servlet通信以及如何让它正常工作。我看过其他帖子,他们让我这么做。

1 个答案:

答案 0 :(得分:1)

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

不要像那样形成网址。而是使用像..

这样的东西
URL urlServlet = new URL(getDocumentBase(), "../servlets/test_servlet");
connection = urlServlet.openConnection();

假设applet位于与examples目录位于同一目录中的目录中。