• Post Reply Bookmark Topic Watch Topic
  • New Topic

connect applet to servlet  RSS feed

Colin Olphert
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:


URL sqlServlet = new URL("");
URLConnection servletConnection = sqlServlet.openConnection();
servletConnection.setRequestProperty("Accept", "text/html");

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

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

}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
Posts: 9436
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
Boost this thread!