How to open a link in a new window using Applet?
Tool Requirements for Preparation: VS Code and Java 8
Establishing the Project Workspace:
- Initiate Visual Studio Code.
- Make a new directory with the name of your choice. We’ll call it “Appletdemo” for the purposes of this example.
- Create a new directory inside “Appletdemo.” Again, you choose the name, but we’ll stick with “appletdemo” to maintain consistency.
File Creation
- Create the following files in the “appletdemo” directory: “my_policy.txt,” “OpenWebPageApplet.html,” and “OpenWebPageApplet.java.”
- Note: You are free to select different names, but make sure to modify the project’s commands before running it.
Keep in mind that naming will be important during the project’s execution phase.
Summary of the project directory
Your project hierarchy ought to look like
Let’s use Applet to produce the essential files needed to open a link in a new window.
- The’my_policy.txt’ file gives the Java applet particular access. Applets’ capabilities are limited by default for security reasons. We use the’my_policy.txt’ file to specifically grant an applet the required permissions if it needs to do anything that isn’t allowed by those default constraints (like accessing a web page).
The ‘my_policy.txt’ file will look like this:
HTML
<!-- save this as 'my_policy.txt' file --> grant { permission java.security.AllPermission; permission java.awt.AWTPermission "showWindowWithoutWarningBanner"; }; |
How to Open a Link in a New Window Using Applet?
A Java applet is a little application created in the Java programming language and run on a web browser while embedded in an HTML page. In essence, it’s a method for introducing Java’s “write once, run anywhere” feature to the world of web browsers.
Components and Organization
- Applets are subclasses of the ‘java.applet.Applet’ class.
- For their graphical user interface, they can use either the AWT (Abstract Window Toolkit) or Swing frameworks, however, AWT has a smaller environmental impact and was previously more popular.