Skip to Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

Contacting a Server on the Applet Host

Problem

You want an applet to contact a socket-based server on the host from which it was loaded.

Solution

Use the method getCodeBase( ) to retrieve a URL for the applet host, and call the URL’s getHost( ). Use this to construct a client socket.

Discussion

For very good security reasons, applets are not permitted network access to servers on hosts other than the one from which the applet was loaded.

To reach a server on the download host, call the applet method getCodeBase( ), which yields a URL for the applet host. Call this URL’s getHost( ) method to get the hostname. Finally, use the hostname to open a client socket (see Section 15.2). For example:

URL u = getCodeBase(  ); 
String host = u.getHost(  ); 
Socket s = new Socket(host , MY_SERVER_PORT);

Of course, in real code you wouldn’t create all those temporary variables:

Socket s = new Socket(getCodeBase().getHost(  ), MY_SERVER_PORT);

And, of course, you will need error handling. Example 17-2 shows an applet that constructs a sort of login dialog and passes the results to a socket-based server on the applet host, using exactly this technique. Figure 17-1 shows the screen display.

Example 17-2. SocketApplet.java

/** Initialize the GUI nicely. */ public void init( ) { Label aLabel; setLayout(new GridBagLayout( )); int LOGO_COL = 1; int LABEL_COL = 2; int TEXT_COL = 3; int BUTTON_COL = 1; GridBagConstraints gbc = new GridBagConstraints( ); gbc.weightx = 100.0; gbc.weighty = 100.0; gbc.gridx = LABEL_COL; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java I/O

Java I/O

Elliotte Rusty Harold
Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham

Publisher Resources

ISBN: 0596001703Catalog PageErrata