• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connect applet to servlet

 
Colin Olphert
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9340
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic