Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are 2 (actually 3) problems:</p> <p>First, an applet is only allowed to fire HTTP requests on the exact URL base as where the applet is been served from. You can obtain it by <a href="http://download.oracle.com/javase/6/docs/api/java/applet/Applet.html#getCodeBase%28%29" rel="nofollow"><code>Applet#getCodeBase()</code></a> which needs to be used as follows:</p> <pre><code>URL url = new URL(getCodeBase(), "hello?query=select * from airports"); URLConnection connection = url.openConnection(); // ... </code></pre> <p>Second, your query string contains illegal characters for use in URLs (space, asterisk). You need to use <a href="http://download.oracle.com/javase/6/docs/api/java/net/URLEncoder.html#encode%28java.lang.String,%20java.lang.String%29" rel="nofollow"><code>URLEncoder#encode()</code></a> to URL-encode the query string.</p> <pre><code>String query = URLEncoder.encode("select * from airports", "UTF-8"); URL url = new URL(getCodeBase(), "hello?query=" + query); URLConnection connection = url.openConnection(); // ... </code></pre> <p>You also need to ensure that you open the HTML/JSP page with the applet in the browser on the same base URL as where the servlet runs. E.g. <a href="http://localhost:8080/hello/pagewithapplet.html" rel="nofollow">http://localhost:8080/hello/pagewithapplet.html</a> and thus not from commandline or by an appletviewer or something. The applet <em>really</em> needs to be served from the same webserver as where the servlet runs.</p> <hr> <p>Unrelated to the concrete problem as stated in the question, your third problem is that sending a plain SQL statement as request parameter is a <em>very</em> bad idea. What if a hacker decompiles your applet and figures how the applet-servlet communication is done and then modifies the SQL statement into something else, such as <code>delete from airports</code>? </p> <p>Do not do the SQL in the applet, do it in the servlet only and let the applet send specific commands only, such as <code>hello?query=list_airports</code> (which is actually still open for further optimization, think of a REST webservice, but that's left up to you as an exercise).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload