August 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Reading Data from Web Pages: Buttons and Text Fields

  • February 20, 2003
  • By Sams Publishing
  • Send Email »
  • More Articles »

Reading Data on the Server

In JSP code, you use the request object's getParameter method to read data from HTML controls. For example, we named the text field in our example text1:

<HTML>
  <HEAD>
    <TITLE>Submitting Text Fields</TITLE>
  </HEAD>

  <BODY>
    <H1>Submitting Text Fields</H1>
    <FORM ACTION="ch04_05.jsp" METHOD="POST">
      Please enter your name:
      <INPUT TYPE="TEXT" NAME="text1">
      <BR>
      <INPUT TYPE="SUBMIT" value="Submit">
    </FORM>
  </BODY>
</HTML>

On the server, you can get the data that the user put in that text field by passing that name to request.getParameter, as you see in Listing 4.5.

Listing 4.5 Reading Data from Text Fields

<HTML>
 <HEAD>
  <TITLE>Reading Data From Text Fields</TITLE>
 </HEAD>

  <BODY>
    <H1>Reading Data From Text Fields</H1>
    Your name is
    <% out.println(request.getParameter("text1")); %>
  </BODY>
</HTML>

That's all it takes—using the expression request.getParameter("text1"), we can retrieve the text in the text field named text1, as you see in Figure 4.5.


Figure 4.5

Reading text from a text field.

That's how it works in general—you can use the request.getParameter method to retrieve data from HTML controls, passing that method the name you've given to the control. In this case, the text field was named text1, and passing that name to request.getParameter returns the data that was in the text field (the data is returned as a Java string).

Using Text Fields

As we've seen, you can retrieve the data in a text field with the request.getParameter method. You can do more than just retrieve the data from controls like text fields; you can also insert data in those controls when you send them back from the server.

In this case, for example, the code will let the user click a button, and when he does, it will display a message in a text field. You can see the HTML page in Listing 4.6.

Listing 4.6 Inserting Text into Text Fields

<HTML>
 <HEAD>
  <TITLE>Inserting Text into Text Fields</TITLE>
 </HEAD>

  <BODY>
    <H1>Inserting Text into Text Fields</H1>
    <FORM NAME="form1" ACTION="ch04_07.jsp" METHOD="POST">
      <INPUT TYPE="TEXT">
      <INPUT TYPE="SUBMIT" VALUE="Click Me!">
    </FORM>
  </BODY>
</HTML>

You see this page in Figure 4.6.


Figure 4.6

Web page with a text field.

When the user clicks the Submit button, you can simply send back the same page from the server, except with the message in the text field, which you can set with the text field's VALUE attribute, as you see in Listing 4.7.

Listing 4.6 Putting Text into Text Fields

<HTML>
 <HEAD>
  <TITLE>Inserting Text into Text Fields</TITLE>
 </HEAD>

  <BODY>
    <H1>Inserting Text into Text Fields</H1>
    <FORM NAME="form1">
      <INPUT TYPE="TEXT" VALUE="Hello from JSP!">
      <INPUT TYPE="SUBMIT" VALUE="Click Me!">
    </FORM>
  </BODY>
</HTML>

You can see the results in Figure 4.7—when the user clicks the button, the message "Hello from JSP!" appears in the text field. This gives your code the feel of client-side programming, but it's really server-side programming—to make that message appear takes a round-trip to the server, which can take time. If you need more immediate results without waiting and don't need to access Java on the server, use a scripting language such as JavaScript in the browser.


Figure 4.7

Inserting text into a text field.

When the user enters text into a text field, you can read that text in your code and compare it against various text strings, and now that we're dealing with the text input, which brings up an important point about Java string handling. To compare two strings, you should use the String class's equals method, not the == relational operator we saw in Day 2. For example, here's how you can check whether the user has typed "JSP rules!" in a text field:

<HTML>
  <HEAD>
    <TITLE>Checking Your Response</TITLE>
  </HEAD>

  <BODY>
    <H1>Checking Your Response</H1>
    <%
    if(request.getParameter("text1").equals("JSP rules!")){
      out.println("You typed JSP rules!");
    }
    else {
      out.println("You didn't type JSP rules!");
    }
    %>
  </BODY>
</HTML>

Tip - If a form only has a text field and no other controls (including no submit button), the browser convention is to submit the data from the text field when the user presses the <Enter> key—no submit button is needed.


Using Text Areas

Text areas are like text fields, except that you can enter multiple lines of text. Also using text areas in code is much like using text fields as well; you can see an example that displays a text area named textarea1—the user can enter text and click the submit button to send that text back to the server—in Listing 4.8.

Listing 4.8 Submitting Text Areas

<HTML>
  <HEAD>
    <TITLE>Submitting Text Areas</TITLE>
  </HEAD>

  <BODY>
    <H1>Submitting Text Areas</H1>
    <FORM ACTION="ch04_09.jsp" METHOD="POST">
      Please enter your text:
      <BR>
      <TEXTAREA NAME="textarea1" ROWS="5"></TEXTAREA>
      <BR>
      <INPUT TYPE="SUBMIT" VALUE="Submit">
    </FORM>
  </BODY>
</HTML>

You can see this text area in Figure 4.8, along with some multi-line text in it.


Figure 4.8

Using a text area.

You can handle that multiline text by displaying it with the line breaks intact in the JSP page. The line breaks in the text from the text area are represented by '\n' characters (see "Determining String Length" in Day 2). That character stands for a line break in text as Java handles it, but the browser will ignore it because it's not HTML. To create line breaks in the browser, we have to replace '\n' characters in the text from the text area with the string "<BR>".

The problem with doing that is that the String class replace method (see Table 2.3) only enables you to replace a single character with another single character (so that the string's length won't change). Instead, we can use the Java StringBuffer class (which you'll find documented in the Java documentation—see "Online JSP Resources" in Day 1), which supports strings you can modify, and whose replace method enables you to substitute one substring for another. On the other hand, the StringBuffer class doesn't have an indexOf method that enables you to search the text for '\n' characters (the StringBuffer methods are geared to modifying text, not searching it). So, to make what we want to happen actually happen, we can start by storing the text from the text area in a StringBuffer object named text:

<HTML>
  <HEAD>
    <TITLE>Reading Text Areas</TITLE>
  </HEAD>

  <BODY>
    <H1>Reading Text Areas</H1>
    You typed:
    <BR>
    <%
    StringBuffer text = new StringBuffer(request.getParameter("textarea1"));
    .
    .
    .

Then, to search for the character '\n', we can create a temporary String object based on text, and use this new object's indexOf method (which returns –1 if '\n' isn't found):

<HTML>
  <HEAD>
    <TITLE>Reading Text Areas</TITLE>
  </HEAD>

  <BODY>
    <H1>Reading Text Areas</H1>
    You typed:
    <BR>
    <%
    StringBuffer text = new StringBuffer(request.getParameter("textarea1"));

    int loc = (new String(text)).indexOf('\n');
    .
    .
    .

All that's left is to replace all '\n' characters with "<BR>" using the text object's replace method, and you can see what that looks like in the while loop in Listing 4.9.

Listing 4.9 Reading Text from Text Areas

<HTML>
  <HEAD>
    <TITLE>Reading Text Areas</TITLE>
  </HEAD>

  <BODY>
    <H1>Reading Text Areas</H1>
    You typed:
    <BR>
    <%
    StringBuffer text = new StringBuffer(request.getParameter("textarea1"));

    int loc = (new String(text)).indexOf('\n');
    while(loc > 0){
      text.replace(loc, loc+1, "<BR>");
      loc = (new String(text)).indexOf('\n');
    }
    out.println(text);
    %>
  </BODY>
</HTML>

Now you're able to display the multiline text from the text area in a Web page using <BR> elements, as you see in Figure 4.9.


Figure 4.9

Reading text from text areas.

Using Password Controls

One of the reasons people use JSP is to authenticate users by checking passwords, and you can use password controls (<INPUT TYPE="PASSWORD">) to let the user enter a password. You can see an example in Listing 4.10.

Listing 4.10 Using Password Controls

<HTML>
  <HEAD>
    <TITLE>Using Password Controls</TITLE>
  </HEAD>

  <BODY>
    <H1>Using Password Controls</H1>
    <FORM ACTION="ch04_11.jsp" METHOD="POST">
      Please enter your password:
      <INPUT TYPE="PASSWORD" NAME="password">
      <BR>
      <INPUT TYPE="SUBMIT" VALUE="Submit">
    </FORM>
  </BODY>
</HTML>

You can see the password control at work in Figure 4.10.


Figure 4.10

Using password controls.

Now you can check the password and respond accordingly, as you see in Listing 4.11.

Listing 4.11 Reading Text from Password Controls

<HTML>
  <HEAD>
    <TITLE>Reading Password Controls</TITLE>
  </HEAD>

  <BODY>
    <H1>Reading Password Controls</H1>
    <%
    if(request.getParameter("password").equals("Open Sesame"){
      out.println("You're in!");
    }
    else {
      out.println("I don't think so!");
    }
    %>
  </BODY>
</HTML>

That's all there is to it, as you see in Figure 4.11, where we've read the password and verified the user.


Figure 4.11

Reading text from a password control.

Using Hidden Controls

Hidden controls let you store text in a Web page that the user doesn't see (unless she specifically views the HTML source for the page). These controls are a good alternative to using cookies to store page-specific data. You can see an example where the code is storing some text in a hidden field in Listing 4.12.

Listing 4.12 Submitting Hidden Fields

<HTML>
  <HEAD>
    <TITLE>Submitting Hidden Fields</TITLE>
  </HEAD>

  <BODY>
    <H1>Submitting Hidden Fields</H1>
    <FORM ACTION="ch04_13.jsp" METHOD="POST">
      <INPUT TYPE="HIDDEN" NAME="HIDDEN" VALUE="Hello from JSP!">
      <INPUT TYPE="SUBMIT" VALUE="Submit">
    </FORM>
  </BODY>
</HTML>

And you can see a JSP page that reads the text in the hidden control in Listing 4.13.

Listing 4.13 Reading Text from Hidden Controls

<HTML>
 <HEAD>
  <TITLE>Reading Hidden Controls</TITLE>
 </HEAD>

  <BODY>
    <H1>Reading Hidden Controls</H1>
    The hidden text is:
    <% out.println(request.getParameter("HIDDEN")); %>
  </BODY>
</HTML>

You can see the results in Figure 4.12.


Figure 4.12

Reading text from a hidden control.

Hidden controls can be very useful in JSP programming, as we'll see in the next topic.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel