June 23, 2018
Hot Topics:

Reading Data from Web Pages: Buttons and Text Fields

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

Submitting Forms

You can submit a form to the server now; this first example will just display a submit button (an <INPUT TYPE="SUBMIT"> element), which HTML uses to send a form's data to the server, as you see in Listing 4.1.

Listing 4.1 Submitting a Form

    <TITLE>Submitting a Form</TITLE>

    <H1>Submitting a Form</H1>
    <FORM NAME="form1" ACTION="ch04_02.jsp" METHOD="POST">
      <INPUT TYPE="SUBMIT" VALUE="Click Me!">

Note that submit buttons don't need to have the caption "Submit"—you can use any caption by setting the VALUE attribute. When the user clicks the submit button, an HTTP request is sent to on the server, which will just acknowledge that the user clicked the submit button, as you see in Listing 4.2.

Listing 4.2 Getting Data From the Server

    <TITLE>Back From the Server</TITLE>

    <H1>Back From the Server</H1>
    You clicked the button!

You can see the HTML page with the submit button in Figure 4.1. When the user clicks that button, an HTTP request is sent to on the server, which returns the Web page you see in Figure 4.2.

Figure 4.1

Using a submit button.

Figure 4.2

Acknowledging an HTTP request.

Sending Data to the Server

The next step is to send some data to the server and read it there. For example, say that you have a text field in a Web page that you want the user to enter his name in, as you see in Listing 4.3.

Listing 4.3 Submitting Text Fields

    <TITLE>Submitting Text Fields</TITLE>

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

Note in particular that this code uses the NAME attribute to give the text field a name, text1. On the server, we'll be able to access the text in the text field using that name. You can see this Web page in Figure 4.3. When the user enters his name and clicks Submit, we'll read the text he has entered back on the server.

Figure 4.3

Submitting text fields.

To read the data in the text field text1, we'll use the built-in JSP request object.

Using request Objects

The request object gives us access to the data sent to us from HTML forms. It's an object of type javax.servlet.http.HttpServletRequest, which is built on top of javax.servlet.ServletRequest, which means the request object includes all the methods in both javax.servlet.http.HttpServletRequest and javax.servlet.ServletRequest.

You can see the methods of javax.servlet.http.HttpServletRequest in Table 4.1, and the methods of javax.servlet.ServletRequest in Table 4.2. Together, these two tables hold the methods available in the response object that we'll be using today. You can see all these methods for the sake of reference—this book will use many, but not all, of them in this and the coming days.

Note that we haven't seen some of the object types returned from these methods yet, such as java.util.Enumeration, but the details on these kinds of objects are coming up soon (we'll see how to use java.util.Enumeration in Day 5, "Reading Data from Web pages: Check Boxes, Radio Buttons, and Select Controls"). Note also the word deprecated in this table, in this sense meaning that an item is considered obsolete. Although still available, deprecated items will be replaced in coming Java versions.

Table 4.1 Methods of javax.servlet.http.HttpServletRequest


Does This

java.lang.Object getAttribute (java.lang.String name)

Returns the value of the named attribute as an object.

java.util.Enumeration getAttributeNames()

Returns an enumeration containing the names of the attributes in this HTTP request.

java.lang.String getCharacterEncoding()

Returns the name of the character encoding used in the body of this request.

int getContentLength()

Returns the length (in bytes) of the body of the HTTP request (or –1 if the length is not known).

java.lang.String getContentType()

Returns the MIME type of the body of the request, or null if the type is not known.

ServletInputStream getInputStream()

Returns the body of the request as binary data using a ServletInputStream (see Day 15 for more on Java readers and streams).

java.util.Locale getLocale()

Returns the preferred locale that the client uses; for example, en_US means US English.

java.util.Enumeration getLocales()

Returns an enumeration of Locale objects indicating the locales that are acceptable to the browser.

java.lang.String getParameter (java.lang.String name)

Returns the value of a request parameter as a String, or null if the parameter does not exist.

java.util.Map getParameterMap()

Returns a Java Map object holding the parameters in this request.

java.util.Enumeration getParameterNames()

Returns an enumeration of String objects containing the names of the parameters contained in this request.

java.lang.String[] getParameterValues (java.lang.String name)

Returns an array of String objects containing all the values the given request parameter has, or null if the parameter does not exist.

java.lang.String getProtocol()

Returns the name and version of the protocol the request uses in this form: protocol/majorVersion.minorVersion (for example, HTTP/1.1).

java.io.BufferedReader getReader()

Returns the body of the request as character data using a Java BufferedReader object (see Day 15 for more on Java readers and streams).

java.lang.String getRealPath (java.lang.String path)

Deprecated. As of version 2.1 of the Java Servlet API, use ServletContext.getReal Path(java.lang.String) instead.

java.lang.String getRemoteAddr()

Returns the Internet Protocol (IP) address that the request comes from.

java.lang.String getRemoteHost()

Returns the name of the client that sent the request.

java.lang.String getScheme()

Returns the name of the protocol used in this request; for example: http, https, or ftp.

java.lang.String getServerName()

Returns the hostname of the server that received the request.

int getServerPort()

Returns the port number on which this request was received.

boolean isSecure()

Returns true if this request was made using a secure channel, such as HTTPS, and false otherwise.

void removeAttribute(java.lang.String name)

Removes an attribute from this request (more on request attributes later).

void setAttribute(java.lang.String name, java.lang.Object)

Stores an attribute in this request (more on request attributes later).

void setCharacterEncoding (java.lang.String env)

Overrides the name of the character encoding used in the body of this request.

Table 4.2 Methods of javax.servlet.ServletRequest


Does This

java.lang.String getAuthType()

Returns the name of the authentication scheme used to protect the page.

java.lang.String getContextPath()

Returns the part of the request URL that indicates the context of the request. We'll see more about request contexts later.

Cookie[] getCookies()

Returns an array containing all the Cookie objects sent with this request.

long getDateHeader(java.lang.String name)

Returns the value of the specified request header corresponding to a Java Date object.

java.lang.String getHeader (java.lang.String name)

Returns the value of the specified request header as a String.

java.util.Enumeration getHeaderNames()

Returns an enumeration of all the header names this request contains.

java.util.Enumeration getHeaders (java.lang.String name)

Returns all the values of the indicated request header as an Enumeration of String objects.

int getIntHeader(java.lang.String name)

Returns the value of the specified request header as an int.

java.lang.String getMethod()

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

java.lang.String getPathInfo()

Returns any extra path information associated with the URL sent for this request.

java.lang.String getPathTranslated()

Returns any extra path information, translating it into a real path.

java.lang.String getQueryString()

Returns the query string that is contained in the request URL after the path.

java.lang.String getRemoteUser()

Returns the login of the user making this request if the user has been authenticated (or null if the user has not been authenticated).

java.lang.String getRequestedSessionId()

Returns the session ID of the request.

java.lang.String getRequestURI()

Returns the part of this request's URL from the protocol name to the query string.

java.lang.StringBuffer getRequestURL()

Returns the URL the client used to make the request.

java.lang.String getServletPath()

Returns the part of this request's URL that calls the servlet.

HttpSession getSession()

Returns the current session associated with this request; or if the request does not have a session, creates one.

HttpSession getSession(boolean create)

Returns the current session associated with this request.

java.security.Principal getUserPrincipal()

Returns the name of the current authenticated user.

boolean isRequestedSessionIdFromCookie()

Indicates whether the requested session ID came from a cookie.

boolean isRequestedSessionIdFromUrl()

Deprecated. As of Version 2.1 of the Java Servlet API, use the isRequestedSessionIdFromURL() method instead.

boolean isRequestedSessionIdFromURL()

Indicates whether the requested session ID came from the request URL.

boolean isRequestedSessionIdValid()

Indicates whether the session ID is still valid.

boolean isUserInRole(java.lang.String role)

Indicates whether the user is included in the specified logical "role" (roles are part of authentication schemes that indicate what group a user is associated with).

The request object holds a great deal of information that is useful to JSP programmers. You can see a sample page that displays some of that information in Listing 4.4.

Listing 4.4 Decoding an HTTP request

    <TITLE>Decoding an HTTP Request</TITLE>

    <H1> Request Information </H1>
    JSP request method: <%= request.getMethod() %>
    URL for the request: <%= request.getRequestURI() %>
    Protocol of the request: <%= request.getProtocol() %>
    Server name: <%= request.getServerName() %>
    Path: <%= request.getServletPath() %>
    Server port: <%= request.getServerPort() %>
    Remote address: <%= request.getRemoteAddr() %>
    Remote host: <%= request.getRemoteHost() %>
    Locale: <%= request.getLocale() %>
    User agent: <%= request.getHeader("User-Agent") %>

You can see the results of this in Figure 4.4, displaying such information as the form method, server port, and so on.

Figure 4.4

Deciphering the request object.

Note in particular the user agent information you see in Figure 4.4, which gives you access to the type of browser the user is using. In Figure 4.4, you see Internet Explorer 6.0, and you can see that in the text returned by the call to request.getHeader("User-Agent"):

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; COM+ 1.0.2204;.NET CLR 1.0.3512)

You can use Java String methods (see Table 2.3) to extract the type and version of the user's browser from this data. Because browsers have different capabilities, this information is often essential when you're creating Web pages to send back to the user—for example, you can tailor your Web page one way for Internet Explorer, and another for Netscape Navigator:

    <TITLE>Determining User Agent</TITLE>

    <H1>Determining User Agent</H1>
    if(request.getHeader("User-Agent").indexOf("MSIE") >= 0){
      out.println("You're using Internet Explorer.");
      //Internet Explorer code.
    if(request.getHeader("User-Agent").indexOf("Netscape") >= 0){
      out.println("You're using Netscape Navigator.");
      //Netscape Navigator code.

Tip - The user agent text is the same as in the userAgent property of the navigator object that you can access using JavaScript in the browser. In Internet Explorer, you'll always find "MSIE" followed by the version of the browser in this text. In Netscape Navigator, you'll find "Netscape" followed by the version number. In earlier versions of Netscape Navigator, you won't find "Netscape"—instead, use the version number, which you'll find right after the text "Mozilla/".

Page 2 of 4

Comment and Contribute


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



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date