 |
Listing 1. Using JSObject, LiveConnect, and HttpURLConnection
import netscape.javascript.*;
class PhoneNumberValidator extends java.applet.Applet
{
java.net.URL ServerURL = new java.net.URL(getParameter("SourceURL"));
public boolean isValidTelephoneNumber()
{
try
{
JSObject DocumentWindow = JSObject.getWindow(this);
JSObject HTMLDocument = (JSObject)DocumentWindow.getMember("Document");
JSObject InputForm = (JSObject)HTMLDocument.getMember("InputForm");
JSObject TelephoneNumberEntry = (JSObject)InputForm.getMember("TelephoneInputField");
JSObject TelephoneHiddenField = (JSObject)InputForm.getMember("TelephoneHiddenField")
TelephoneHiddenField.setMember("value",
getStandardizedTelephoneNumber(TelephoneNumberEntry.getMember("value")));
return true;
}
catch(InvalidObjectException ex)
{
return false;
}
}
// Note: there's a lot left out here in terms of Content factories
// the whole server-side interface, etc...
private String getStandardizedTelephoneNumber(String IncomingNumber) throws InvalidObjectException
{
java.net.HttpURLConnection NewConnection = new java.net.HttpURLConnection(ServerURL);
NewConnection.setRequestMethod("POST");
NewConnection.setRequestProperty("TelephoneNumber", IncomingNumber);
NewConnection.setDoInput(true);
NewConnection.connect();
Object ReturnedValue = NewConnection.getContent();
if(ReturnedValue instanceof String)
return((String)ReturnedValue);
else
throw new InvalidObjectException("Server Request returned an invalid object");
}
}
The JavaScript Code on the HTML page:
<APPLET
CODE="PhoneNumberValidator.class"
width=0
height=0
name="PhoneNumberValidatorClass"
CODEBASE="http://www.someplace.com/someapp/applets/validators.jar">
</APPLET>
<FORM name="InputForm" method="GET" action="CreateUser.jsp">
<P>Enter a telephone number:</P>
<INPUT TYPE="text" name="TelephoneInputField" LENGTH=20
onChange="document.PhoneNumberValidatorClass.isValidTelephoneNumber()">
<INPUT TYPE="hidden" name="TelephoneHiddenField" >
</FORM>
|
Return to article
|  |
|