I have created an application and I'm turning it into an applet. It pulls html source from a website by using the openStream() method of the URL object. It parses through that html source and extracts sports scores. This works as a free standing application but when I try to run it as an applet, I get what appears to be an AccessControlException that appears to be thrown when the URL.openStream() method is envoked. If I'm not allowed to perform any I/O operations within an applet, how do i go about pulling the HTML source to parse through it? Thanks, Wes
Welcome to Java Ranch! An unsigned applet can't connect to any server except the one that served it; this prevents all sorts of devious security breaches (imagine I'm inside a firewall, and download an applet from www.blackhat.com, which then starts connecting to all sorts of machines inside my firewall!) A signed applet can be granted this permission. Signing applets is tricky, browser/plugin-dependent business, however. Are you sure you don't want to do the work in a servlet, and just display the results in a very simple applet, if needed? You'll save yourself a lot of headaches. [ September 03, 2003: Message edited by: Ernest Friedman-Hill ]
Thanks for such a prompt response first off. Ideally I'd like it to be just an applet with no servlets involved that I could just plug into any web page. However, I'm very opened minded and would like to hear how to use the servlet method you mention. I know how to write a servlet but how would you pass the string of parsed HTML from the servlet to the applet? Could I do the same using a JSP page instead of a servlet? I'd really like to hear how to do this or if you had a page or site that explains it you could just give me the URL and save some typing. Once again, thanks for the help... Wes
author and iconoclast
The applet could connect to the servlet to obtain the information, in exactly the same way that you were originally going to get the information directly from some other server. The servlet could even simply just forward the applet's request to the other server. JSPs aren't too well suited for this if you're foing to use an applet, as their output is an HTML page, and presumably you'd want to parse the data on the server and let the applet just display it. Alternatively, you could dispense with the applet altogether, and have a JSP fetch the info and display it as HTML. I can't think of any specific site that deals with what you want to do, exactly. For learning about servlets and JSPs in general, I really liked Wrox's "Professional Java Servlets 2.3". You could direct specific questions to the Servlets or JSP forums.
Joined: Sep 03, 2003
Alright, I'm back. Thanks for all your help. I'm a little rusty on the Servlets so if you could give me a hand I'd appreciate it. In the servlet, I just create a new URL object which is the site I want to pull the html from then I open the stream on that URL object and store the html read from the InputStream in a String variable. I then create a new PrintWriter object by calling the response.getWriter() and send the html source back to the applet by calling the println() method on the PrintWriter and pass the html source string into that println() method. How does that sound so far? Would that return the html source to the applet? My next problem is how would my applet accept the string from the servlet. I'd create a string variable in the applet as well and set it equal to what? Basically, how would I call the servlet from the applet? If you could provide some code example, that would be superb. If I'm still sounding a little sketchy or I'm not being clear enough, please let me know. Thanks again, Wes
Hi Wesley You can open a URLConnection from the applet to the servlet and then from that connection you can obtain output and input streams and you will be 'GOD' Now, in your case you want to write a String from the servlet to applet I guess, so either you can just do like, BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String response = br.readLine(); and that should do the job. If you want to write complex objects back to the applet (given they are serializable and satisfying all necessary requirements) then you could just wrap the urlConnection.getInputStream() into ObjectInputStream....rest all is just streams once you get InputStream from the urlConnection.. Although, we have to keep the Applet restriction in mind that forces applet to make connections only to the server where it came from which I believe you already know... Regards Maulin