从 Canvas 元素查询 MySQL 数据库?
好的,所以我想尝试找出制作在浏览器中运行的简单 2D 游戏的最佳方法。我研究过 HTML5 和 Canvas 元素。不过,您似乎使用了 JavaScript,而且我读到您无法从 JavaScript 连接到 MySQL 数据库,因此从 Canvas 元素(似乎使用 JavaScript)进行连接似乎是不可能的。
我还一直在研究在浏览器中运行 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);
}
它给了我“无法打开 URLConnection,错误:http://localhost:8080/examples/servlets/test_servlet" 响应。我认为这可能与我保存 Servlet 文件的方式/位置有关。我只是把它放在 example/servlet 文件夹中。这对我来说非常令人困惑,我试图弄清楚这个 Applet<->Servlet 通信以及到底如何让它工作。我看过其他帖子,他们已经让我到目前为止了。
Ok So I'd like trying to figure out the best way to go about making a simple 2D game that runs in the browser. I've looked at HTML5 and the Canvas element. It seems you use JavaScript though and I've read you can't connect to a MySQL database from JavaScript so connection from a Canvas element, which seems to use JavaScript, seems to be out of the picture.
I've also been looking into running an Applet in the browser and communicating with a Servlet that then connects to the browser and then relays info from the database back to the Applet. I read this is the better way to do it to avoid connecting strait from the applet which is insecure and potentially allowing people access to the database. I'm not quite sure how they would go about injecting code and connecting to my database so if anyone could shed light on that topic I'd be grateful. Anyway I'm having trouble getting my Servlet to working. I'm using Tomcat and using the following code in my 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());
}
}
My Servlet has the following code in it:
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);
}
It is giving me the "Couldn't open a URLConnection, error: http://localhost:8080/examples/servlets/test_servlet" response when I try to compile the Applet in Eclipse. I assume it probably has something to do with how/where I saved my Servlet file. I just threw it in the examples/servlet folder. This is all very confusing for me and I'm trying to figure out this Applet<->Servlet communication and how exactly to get it working. I've looked at other posts and they have gotten me this far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要这样形成 URL。相反,使用类似......的内容,
假设小程序位于与
examples
目录相同的目录中。Don't form the URL like that. Instead use something like..
That presumes the applet is in a directory that is in the same directory as the
examples
directory.