SDeprez
asked on
URLConnection/getOutputStream in Applet init/start
I have attempted to make a URLConnection to a servlet at the same address as the servlet that launched an applet(I have confirmed the address via getDocumentBase, both are on my PC) in the applet's init and start methods. The connection appears to work but when I do a getOutputStream (also in the applet's init and start methods) on the URLConnection I get a SecurityException (cannot access localhost). An URLConnection/getOutputStr eam to the same servlet works in a method that executes after init and start have completed.
ASKER
getCodeBase returns the directory that the applet was loaded from (i.e. file://D:/Projects/project name). In this case, getCodeBase().getHost() isn't returning anything. getDocumentBase returns the URL for the servlet that loaded this applet (i.e. http://localhost:8080/servlet/servletname). The servlet I'm connecting to has the same URL for the exception of the servletname.
Note one point:-
The only way the applet will connect to servlet unless it is signed is with
server name as getCodeBasse().getHost().
Since U are not using webserver , U are getting d:\..\..\.
Don't run applet like that.
Load Your applet from webserver
Then only it will work.
Applet when executing in browser will feel localhost as also as remove server.
It won't know that that is the server from which it is downloaded.
The only way the applet will connect to servlet unless it is signed is with
server name as getCodeBasse().getHost().
Since U are not using webserver , U are getting d:\..\..\.
Don't run applet like that.
Load Your applet from webserver
Then only it will work.
Applet when executing in browser will feel localhost as also as remove server.
It won't know that that is the server from which it is downloaded.
ASKER
Adjusted points from 50 to 75
ASKER
Then why does this work ok when I put the URLConnection/getOutputStr eam in an applet method other than init or start?
Are U using Browser or AppletViewer??
IE or NEtscape ??
I told all the matter for browsers.
ASKER
I'm using IE with servletrunner.
I'm afraid I don't totally understand your answer. Are you saying that since init and start are invoked by the browser (and the other methods are not) that the URLConnect/getOutputStream work differently?
I do appreciate your assistance!!!
I'm afraid I don't totally understand your answer. Are you saying that since init and start are invoked by the browser (and the other methods are not) that the URLConnect/getOutputStream
I do appreciate your assistance!!!
once an applet connected to servlet,
it won't connect again. It will give U exception.
getCodeBase() like functions are available after applet is inited.
it won't connect again. It will give U exception.
getCodeBase() like functions are available after applet is inited.
ASKER
I think you just answered my question. Let me know if I understand you correctly. In a nutshell, getCodeBase, URLConnection, etc. are not available until after an applet's start and init methods have completed?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.j-nine.com/pubs/applet2servlet/
Solution:-
int servletport = 8080; //Replace Ur servlet port
URLConnection con = new URL("http://"+getCodeBase().getHo
"/servlet/YourServlet").op
i.e just replace
localhost
with
getCodeBase().getHost()