gpt4 book ai didi

java - 从 Canvas 元素查询 MySQL 数据库?

转载 作者:行者123 更新时间:2023-11-29 14:41:45 24 4
gpt4 key购买 nike

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

我还一直在研究在浏览器中运行 Applet 并与 Servlet 通信,然后连接到浏览器,然后将信息从数据库中继回 Applet。我读到这是更好的方法,以避免从小程序连接海峡,这是不安全的,并且可能允许人们访问数据库。我不太确定他们将如何注入(inject)代码并连接到我的数据库,因此如果有人能够阐明该主题,我将不胜感激。不管怎样,我在让我的 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 文件的方式/位置有关。我只是把它放在 example/servlet 文件夹中。这对我来说非常令人困惑,我正在尝试弄清楚这个 Applet<->Servlet 通信以及如何让它正常工作。我看过其他帖子,他们已经让我到目前为止了。

最佳答案

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 目录位于同一目录中。

关于java - 从 Canvas 元素查询 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com