Home arrow Java & J2EE arrow Page 3 - The JSP Files (part 4): The Red Pill

Entering The Matrix - Java

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.

TABLE OF CONTENTS:
  1. The JSP Files (part 4): The Red Pill
  2. The Last Action Hero
  3. Entering The Matrix
  4. Requesting More
  5. Taking Some Medication
  6. What's For Dessert?
  7. A Chocolate Addiction
  8. Couch Potato
  9. Beating It Into Submission
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
March 07, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.


<form method="POST" action="matrix.jsp"> ...</form>
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>


 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: