Add code to an applet to display the response from a servlet.
Displaying Java Servlet Response
Displaying Java Servlet ResponseThe applet is really coming together now. You have seen how to react to the user clicking the button, how to build the URL that will cause the browser to send a GET to the servlet, and
how to code the servlet so that it sends back a string in response to the GET. You might ask,
What will the applet do with that string when the servlet sends it back?
There are lots of possibilities. You could display a message box, change the Enter your name label on the surface of the applet, or draw some text directly on the surface of the applet. For the purpose of example, we will create another label, and change the text of this output label from blank to the greeting you get
from the servlet. The following SlideShow demonstrates the overall effect.
We should warn you right now that this example will remain hypothetical. We will use it solely as a reference point for
discussing the issues involved in applet-server communication. But do not worry,
there is plenty of code later in the chapter that demonstrates the techniques discussed here, just in somewhat simpler examples
To add another label, I added it as a member variable. In init() I set its value to many spaces.
As part of actionPerformed(), I changed its value with setText().
Here are the relevant changes to the applet
public class Getter extends Applet implements ActionListener{
private TextField input;
private Label output = new Label(" ");
public void init() {
// beginning of init() is unchanged
add(output);
}
// --- actionPerformed
public void actionPerformed(ActionEvent event) {
// send request to servlet and get response into "reply" variable
output.setText(reply);
}
}
More Complex Applet
This particular applet just displays what the servlet sends. A fancier one might do something different with it.
For example, the servlet might send a number, and the applet might use that to decide what color to draw something in.
Or two numbers might be an x and y co-ordinate. We just dump the string into a label, but the only limit to the ways that applets and servlets can work together is your imagination.
Trusted and Untrusted Applets:
When a Java applet is embedded in a web page, a browser can download it and execute it automatically. If you think about it, that is a very dangerous thing to do. So, to protect the client, JDK 1.0 assumed all applets were untrusted and ran them under the watch of a SecurityManager that severely limited what they could do.
For example, the security manager made sure applets couldnât write to the userâs file system, read certain system properties, accept incoming socket connections, or establish outgoing socket connections to any host but the origin server. This protected the client, but it limited the usefulness of applets. Consequently, JDK 1.1 introduced the concept of trusted applets, applets that can operate like normal applications with full access to the client machine. For an applet to be trusted, it has to be digitally signed by a person or company the client trusts (as marked in the clientâs browser). The signature authenticates the applet's origin and guarantees integrity during the transfer, so the client knows the applet code has not been surreptitiously changed. This allowed for more productive applets, but it was an all-or-nothing approach.
To give the client more control, JDK 1.2 is introducing a fine-grained access control system.
Under this new system, a digitally signed applet can be partially trusted, given certain abilities without being given free reign on the system. This promises to allow applets from unknown sources to be granted small privileges (such as writing to a single directory), without granting them the ability to wipe the clientâs hard drive.
All the spaces in the initial value of the label are to ensure that enough room is left on the applet surface for the longer strings the label will hold later.
Learn how to read the string from the servlet in the next lesson.