• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

connect applet to servlet

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm writing an applet that sends an SQL statement to a servlet accross a network and then receives the results as a vector. Here is the code I use:

try{

URL sqlServlet = new URL("http://193.61.149.35/smarthome/SmartHomeSQLServer");
URLConnection servletConnection = sqlServlet.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setRequestProperty("Accept", "text/html");
servletConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

// Send POST output.
OutputStreamWriter printout = new OutputStreamWriter(servletConnection.getOutputStream());
String sql = "SELECT * FROM test WHERE number='1';";
String content = "SQL=" + URLEncoder.encode(sql);
printout.write(content);
printout.flush();

// Get the response
ObjectInputStream ois = new ObjectInputStream(servletConnection.getInputStream());
Vector v = (Vector) ois.readObject();
printout.close();
ois.close();

}catch( Exception E){

JOptionPane.showMessageDialog(null, E.getMessage());}

The problem is that when the line: OutputStreamWriter printout = new OutputStreamWriter(servletConnection.getOutputStream()); is executed an exception is thrown. The exception is: access denied(java.net SocketPermission)

Can anyone tell me why this is happening and how I can fix it.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Javaranch, Colin.
What usually causes a SecurityException when opening a socket in an applet is the fact that applets are not allowed to contact servers other than the one they were downloaded from. The Java Tutorial has a good discussion about the security restrictions on applets in the chapter Practical Considerations when Writing Applets. It also describes techniques to work within the restrictions.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic