Java 从Canvas元素查询MySQL数据库?
好的,我想找出制作一个在浏览器中运行的简单2D游戏的最佳方法。我已经看过HTML5和Canvas元素。看起来您使用的是JavaScript,而我已经读到您无法从JavaScript连接到MySQL数据库,所以从画布元素(似乎使用JavaScript)进行连接似乎是不可能的 我还研究了在浏览器中运行小程序,并与Servlet通信,Servlet随后连接到浏览器,然后将数据库中的信息传递回小程序。我认为这是一种更好的方法,可以避免连接到applet,因为applet不安全,可能允许人们访问数据库。我不太确定他们将如何注入代码并连接到我的数据库,所以如果有人能解释这个话题,我将不胜感激。无论如何,我很难让Servlet正常工作。我正在使用Tomcat并在小程序中使用以下代码:Java 从Canvas元素查询MySQL数据库?,java,mysql,servlets,applet,communication,Java,Mysql,Servlets,Applet,Communication,好的,我想找出制作一个在浏览器中运行的简单2D游戏的最佳方法。我已经看过HTML5和Canvas元素。看起来您使用的是JavaScript,而我已经读到您无法从JavaScript连接到MySQL数据库,所以从画布元素(似乎使用JavaScript)进行连接似乎是不可能的 我还研究了在浏览器中运行小程序,并与Servlet通信,Servlet随后连接到浏览器,然后将数据库中的信息传递回小程序。我认为这是一种更好的方法,可以避免连接到applet,因为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);
}
公共类测试\u servlet扩展了HttpServlet{
受保护的void processRequest(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
setContentType(“text/html;charset=ISO-8859-1”);
PrintWriter out=response.getWriter();
字符串缺陷=request.getParameter(“缺陷”).toString();
试一试{
out.println(“”);
out.println(“”);
out.println(“Servlet”);
out.println(“”);
out.println(“”);
println(“Servlet at”+request.getContextPath()+”);
out.println(“缺陷:“+Defect+””);
out.println(“”);
out.println(“”);
}最后{
out.close();
}
}
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
processRequest(请求、响应);
}
@凌驾
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
试一试{
setContentType(“application/x-java-serialized-object”);
InputStream in=request.getInputStream();
ObjectInputStream inputFromApplet=新ObjectInputStream(in);
String servletText=“来自Servlet的文本”;
//将其回显到applet
OutputStream outstr=response.getOutputStream();
ObjectOutputStream oos=新的ObjectOutputStream(outstr);
oos.writeObject(servletText);
oos.flush();
oos.close();
}捕获(例外e){
e、 printStackTrace();
}
processRequest(请求、响应);
}
它给了我“无法打开URL连接,错误:”当我尝试在Eclipse中编译小程序时,会有响应。我认为这可能与我保存Servlet文件的方式/位置有关。我只是将其扔到了examples/Servlet文件夹中。这对我来说非常混乱,我正试图弄清楚AppletServlet通信以及如何使其工作。我已经查看了其他帖子和你让我走了这么远
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();
这假定小程序所在的目录与
示例
目录位于同一目录中。“这让我非常困惑”在这种情况下,混合applet、servlet和DBS会给你带来很多麻烦。我建议在步骤1中考虑如何将Servlet连接到DB。只有当它工作时,考虑将Applet或JS扔到混合中。(BTW——如果一个applet可以连接到servlet,那么JS也可以)。@AndrewThompson嗯,这样我就可以继续使用Javascript/Canvas,在JS和servlet之间进行通信,允许servlet与DB进行通信,并用所需的信息回复JS?我不建议在servlet工作之前做任何事情。顺便说一句,。/
部分不是必需的。