?
Solved

URLConnection/getOutputStream in Applet init/start

Posted on 2000-05-12
11
Medium Priority
?
597 Views
Last Modified: 2008-02-26
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/getOutputStream to the same servlet works in a method that executes after init and start have completed.
0
Comment
Question by:SDeprez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2804855
Visit this for more info

http://www.j-nine.com/pubs/applet2servlet/ 

Solution:-

int servletport = 8080; //Replace Ur servlet port
URLConnection con = new URL("http://"+getCodeBase().getHost()+":"+servletport+
"/servlet/YourServlet").openConection();

i.e just replace

localhost

with

getCodeBase().getHost()
0
 

Author Comment

by:SDeprez
ID: 2804981
getCodeBase returns the directory that the applet was loaded from (i.e. file://D:/Projects/projectname).  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.
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2805014
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SDeprez
ID: 2805904
Adjusted points from 50 to 75
0
 

Author Comment

by:SDeprez
ID: 2805905
Then why does this work ok when I put the URLConnection/getOutputStream in an applet method other than init or start?
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2805967

Are U using Browser or AppletViewer??

IE or NEtscape ??
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2805971
I told all the matter for browsers.
0
 

Author Comment

by:SDeprez
ID: 2806012
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!!!
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2806377
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.
0
 

Author Comment

by:SDeprez
ID: 2810863
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?  
0
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 150 total points
ID: 2811630
Applet has no constructors:- ( We won't use but we can use and not recommonded..)

>The browser invokes your constructor, then setStub, then init(). Hence when your constructor is
invoked, the AppletStub (and through it the AppletContext) is not yet available. Although in principle you
can do things in the constructor that don't  rely (even indirectly) on the AppletStub or AppletContext, it is
less error-prone to simply defer all setup to the init method. That way you know that anything that needs
the stub/context will have it available.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question