Now that the basics are done with, it's time to begin applyingthat theory to Web development. This week, The JSP Files investigates theJSP Request object, and demonstrates how it can be used to process formdata. Learn about JSP's array variables, find out what chocolate chipcookies and couch potatoes have in common, and explore a very unusualpharmacy.
Once the form has been submitted, the script "matrix.jsp" is called upon to parse the date entered into the form. At this point, the script simply reads the name entered into the form, and displays a message containing that name; however, at a later point, it will be modified to grant or deny access based on the name entered.
<html>
<head>
<basefont face="Arial">
</head>
<body>
<center>
<%
// matrix.jsp
// define the variables used in the scriptlet
String fname;
// assign values
fname = request.getParameter("name");
// print the details
out.println("Welcome to The Matrix, " + fname + "!");
%>
</center>
</body>
</html>
And now, if you enter some data into the form (say, "joe"), this is what you should see:
Welcome to The Matrix, joe!
An explanation is in order here. As always, the first step is to define the variables that will be used throughout the script - in this case, the variable "fname".
<%
// define the variables used in the scriptletString fname;%>
Next, the value of the form variable "name" has to be assigned to the JSP variable "fname" - this is accomplished with the getParameter() method, which accepts a variable name as parameter and returns the variable value.
The getParameter() method actually belongs to a JSP object called the Request object; unlike many other objects in JSP, the Request object is an "implicit" object, so called because you do not need to explicitly create an instance of the object when you want to use it. The getParameter() method is just one of many methods available in this object, and we'll be exploring some of the others as well in this tutorial.
Once the value of a form variable has been assigned to a JSP variable, it can be treated in exactly the same manner as other JSP variables. In the example above, a println() function call takes care of printing the welcome string, with the name incorporated into it.
You can also use the POST method (which offers greater security and reliability) to process form data - simply alter the HTML form so that the METHOD used is POST.
The script "matrix.jsp" will continue to function as advertised without requiring any changes. Thus, the getParameters() method can be used to access form variables regardless of the method used to post the data.
And you can add a simple conditional statement to deny access to all but the most favoured:
<html>
<head><basefont face="Arial"></head><body><center><%// matrix.jsp// define the variables used in the scriptletString fname;// assign valuesfname = request.getParameter("name");// print the detailsif (fname.equals("neo")){out.println("Welcome to The Matrix, Neo!");}else{out.println("Leave immediately, Agent!");}%></center></body></html>