Java Reference
In-Depth Information
The following step enters the code for the setAction() user-defined method.
To Create the setAction() Method
1. Enter lines 35 through 40 as shown in Figure 12-65 on page 841.
TextPad displays the JavaScript function, setAction(), in the coding window
(Figure 12-68). The parameter value is assigned to a hidden field in the
main form and then the form is submitted to the server.
setAction()
JavaScript
function
value assigned to
hidden form field
form submit()
built-in function
FIGURE 12-68
An additional JavaScript function is needed to alert users if their passwords
are soon to expire. This alert displays when the current page initially is loaded
for the user. To function properly, data from the server is needed to determine
when to trigger the alert message.
Using Session Data in a JSP Scriptlet
Transmitting data from a form to the server is straightforward. As discussed
earlier, when a form is submitted, the form field data is passed as name=value
parameters to the form-processing program on the server. The field value can be
set either programmatically or by input from the user. A program on the server,
whether a servlet, CGI, or JSP, can obtain the parameter data and use it to create
a Web page dynamically as a reply to the browser, as you have done already. If
that dynamic Web page must again send parameter data to the server, the Web
page is created with a form and form fields. When the new form is submitted,
the form field data is transmitted to the Web server again.
Unfortunately, this can result in a large amount of data passing back and
forth between the browser and the server and thus can degrade performance.
Rather than passing data to the Web page to be processed by JavaScript code, a
JSP can use data on the server to create a Web page dynamically, just as any other
servlet. Recall that a JSP has access to an implicit session object stored on the
server. Lines 41 through 55 in Figure 12-65 define a function, checkUses(), to
display a warning if the password is expiring. The embedded JSP uses the session
object to check an attribute, uses, which is set by the servlet. When servlet is
called again, it will remove this attribute so the message displays only when the
user first logs on and only if his or her password is expiring.
Search WWH ::




Custom Search