且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行浏览器中的小应用程序的jdbc

更新时间:2023-11-19 21:37:58

作为评论别人,你真的不希望这样做。

As commented by others, you really don't want to do this.

刚刚创建的服务器端web服务(它可以是一个普通的servlet的),并使用的 java.net.URLConnection中的 中的applet。

Just create a webservice in the server side (which can be a plain vanilla servlet) and make use of java.net.URLConnection in the applet.

基本的servlet例如:

Basic Servlet example:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action"); // Or request.getPathInfo() whatever you want.
    String result = someDAO.doAction(action);
    response.getWriter().write(result);
}

基本Applet的例子:

Basic Applet example:

URL url = new URL("http://example.com/databaseservlet?action=someaction");
URLConnection connection = url.openConnection();
InputStream result = connection.getInputStream(); // Important. This actually fires the request!

小心 SQL注入然而的。不要以任何方式通过原始的SQL查询请求参数或PATHINFO和使用的 preparedStatement 在DAO code所有的时间。

Be careful with SQL injections however. Do in no way pass raw SQL queries as request parameters or pathinfo and use PreparedStatement all the time in the DAO code.

由于响应数据格式,你可以使用一个普通的字符串(如实例)或一张 XML字符串 JSON字符串或者甚至一个fullworthy的Java与系列化的一点点帮助对象。

As response data format you can use a plain vanilla String (as given in example) or a XML string or a JSON string or maybe even a fullworthy Java object with a little help of Serialization.