Reading Data from HTML Controls in JSP


Take a look at the chat room in Figure 5.1; you can see various HTML controls there, namely the text field where the users type their name, the text area where they type their comments, and the Submit button. How do those controls send data to your JSP page?

HTML controls must be enclosed in HTML forms, which you create with the <FORM> element. For example, if you want to add a text field to a web page using the <INPUT TYPE="TEXT"> element, that element must be inside a form. Here's what that looks like in an HTML example, ch05_04.html:

 <HTML>     <HEAD>         <TITLE>Enter your name</TITLE>     </HEAD>     <BODY>         <H1>Enter your name</H1>         <FORM NAME="form1" ACTION="ch05_05.jsp" METHOD="POST">             <INPUT TYPE="TEXT" NAME="text">             <INPUT TYPE="SUBMIT" VALUE="Submit">         </FORM>     </BODY> </HTML> 

You can see this web page in Figure 5.6.

Figure 5.6. Entering data to send to the server.


You use the ACTION attribute to specify to where the data in the controls in a form should be sent when the form's Submit button is clicked. For example, to send the data in a form to http://www.edwardschat.com/jsps/chat.jsp, you'd set the ACTION attribute to that URL:

 <FORM NAME="form1"     ACTION="http://www.edwardschat.com/jsps/chat.jsp" METHOD="POST">     <INPUT TYPE="TEXT" NAME="text">     <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> 

You can also specify a relative URL, which means relative to the directory that the current document is in. For example, to send a form's data to chat.jsp in the same directory as the current document, you can set the ACTION attribute this way:

 <FORM NAME="form1" ACTION="chat.jsp" METHOD="POST">     <INPUT TYPE="TEXT" NAME="text">     <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> 

You can also omit the ACTION attribute, which is how chat.jsp does it. If you do, the data in the form is simply sent back to the exact same URL as the current document.

The METHOD attribute lets you specify the way you send the form's data back to the server. There are two possible settings for this attribute"GET" and "POST"and JSP can handle both.

If you use the GET method, the data in the form is treated as text and appended to the URL that the browser navigates to. The server will decode that data appended to the URL and pass it on to the JSP code.

You can also use the POST method, which works just as well as GET as far as we are concerned, but encodes the data as part of the actual Hypertext Transfer Protocol (HTTP) request sent to the server. This means that the data is not visible to the user in the browser.

That sets up the form, but what about actually reading data from the HTML controls? For that, you use request objects.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net