Java
  Home arrow Java arrow Page 9 - The JSP Files (part 4): The Red Pill
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 4): The Red Pill
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2001-03-07

    Table of Contents:
  • The JSP Files (part 4): The Red Pill
  • The Last Action Hero
  • Entering The Matrix
  • Requesting More
  • Taking Some Medication
  • What's For Dessert?
  • A Chocolate Addiction
  • Couch Potato
  • Beating It Into Submission

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 4): The Red Pill - Beating It Into Submission


    (Page 9 of 9 )

    You'll have noticed that in all the examples we've shown you thus far, we've used two pages - a single HTML page containing the form, and a separate JSP script which processes the form input and generates appropriate output. However, JSP provides an elegant method to combine those two pages into one via the form's SUBMIT button.

    You've already seen that once a form is submitted to a JSP script, all the form variables become available to JSP. Now, in addition to the user-defined variables, each time you hit the SUBMIT button on a form, a variable named "submit" is created. And by testing for the presence or absence of this variable, a clever JSP developer can use a single JSP document to generate both the initial form and the output after it has been submitted.

    The following code snippet demonstrates how the "welcome to The Matrix" example above could be rewritten using this technique.



    <html> <head> <basefont face="Arial"> </head> <body> <center> <% // matrix.jsp // check for submit variable String submit = request.getParameter("submit"); if(submit != null) { // form has been submitted, display result // 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 + "!"); } else { // display initial form %> <form method="GET" action="matrix.jsp"> <table cellspacing="5" cellpadding="5" border="0"> <tr> <td> <font size="-1">Name, rank and serial, number, soldier!</font> </td> <td align="left"> <input type="text" name="name" size="10"> </td> </tr> <tr> <td colspan="2" align="center"> <input name="submit" type="submit"> </td> </tr> </table> </form> <% } %> </center> </body> </html>
    As you can see, the script first tests for the presence of the "submit" variable - if it doesn't find it, it assumes that the form has yet to be submitted and so displays the initial form.

    Since the ACTION attribute of the
    tag references the same JSP script, once the form has been submitted, the same script will be called to process the form input. This time, however, the "submit" variable will exist, and so JSP will not display the initial page, but rather the result page.

    Note that for this to work, your

    <input type="submit">
    must have a NAME attribute with the value "submit", like this:

    <input type="submit" name="submit">
    And that's about it. Next time, we'll be hooking JSP up to a database in order to generate dynamic Web page - so don't miss that one. Until then...stay healthy!

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway