Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Applet-CGI file i/o -- Please Help!!?  RSS feed

Mahesh Srinivasan
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have set up a java applet for my group�s website and am trying to figure out how to send applet data (write a file) to the server. I have written a cgi script which takes a string and writes it to a file, and I know that this script functions properly since it works when I run it in the console.
Essentially, the problem is that in order to access the cgi program, I have to connect a URL beginning with: This is a problem because java�s security restrictions only allow you to send data to programs originating from the same code base (e.g. So when I try to send data to an exception is raised called an "access control exception" from the following code:

public void SendData(String data) {

try {
URL url = new URL("http", "", "/cgi-bin/write.cgi");
URLConnection con = url.openConnection();
con.setRequestProperty("Content-type", "text/plain");
con.setRequestProperty("Content-length", data.length()+"");
PrintStream out =
new PrintStream(con.getOutputStream(), true);

DataInputStream in =
new DataInputStream(con.getInputStream());
String s;
while ((s = in.readUTF()) != null) {

catch (MalformedURLException mue) {
catch (IOException ioe) {

catch (Exception e) {

Do you have any idea how I can fix this problem, or know anyone that could possibly help? I would be very grateful if you could help.

Thanks a lot,
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I need to do server work from an applet, I'd usually have the applet call a servlet that exists in the same base as the applet, and have it do all the work. Avoids the applet restriction mess. Though someone else might have a better idea.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!