Using Request Objects


When the user clicks the Submit button in ch05_04.html, the ACTION attribute in the HTML makes the browser send the data in the HTML controls to ch05_05.jsp:

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

In ch05_05.jsp, the request object gives you access to the data sent from the HTML form. This object is built in to the JSP framework, much like the out object, so it's already available to you as soon as your code starts. It's an object of the javax.servlet.http.HttpServletRequest class, and you can see the significant methods of javax.servlet.http.HttpServletRequest in Table 5.1.

Table 5.1. Significant Methods of the javax.servlet.http.HttpServletRequest Class

Method

Does This

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

Returns the value of the given named attribute as a Java object

java.util.Enumeration getAttributeNames()

Returns an enumeration that holds the names of the attributes in this request

int getContentLength()

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

java.lang.String getContentType()

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

ServletInputStream getInputStream()

Returns the body of the request as binary data using a ServletInputStream stream

java.util.Locale getLocale()

Returns the preferred locale that the client uses; for example, en_US means U.S. 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 object. Returns null if the parameter does not exist

java.util.Enumeration getParameterNames()

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

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

Returns an array of String objects contain ing the values the given request parameter has. Returns null if the parameter does not exist

boolean isSecure()

Returns TRue if this request was made using a secure channel, such as HTTPS. Returns 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 o)

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


In JSP code such as chat.jsp, you use the request object's getParameter method to read data from HTML controls. For example, the text field in ch05_04.html is named text:

 <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 get the data the user entered in that text field on the server by passing that name to the request.getParameter method, as you see in ch05_05.jsp:

 <HTML>   <HEAD>     <TITLE>Reading Text Using Text Fields</TITLE>   </HEAD>     <BODY>         <H1>Reading Text Using Text Fields</H1>         Your name is         <% out.println(request.getParameter("text")); %>    </BODY> </HTML> 

That's all it takesusing the Java expression request.getParameter("text"), you can retrieve the text in the text field named text. Enter ch05_05.jsp, and store it as webapps\chat\ch05_05.jsp. Now when you enter your name in ch05_04.html and click the Submit button, that name is sent to ch05_05.jsp, which displays it, as you see in Figure 5.7.

Figure 5.7. Reading text from a text field.


That's how it worksyou 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).

At this point, you have nearly all the technology needed to create chat.jsp; you can accept input from the user and display results. The chat.jsp application needs more power, however. Take a look at Figure 5.1, which shows Chat in action. Chat has to not only read input from the user, it has to display the current user comments, which means storing those comments on the server.

Your first thought might be to store those comments in a set of files and to treat those files as a sort of stack where the oldest comment is popped to make room as the most recent comment is pushed onto the stack. However, working with files in a multiuser web server environment is inherently prone to difficultiesin particular, what if you're writing to a file at the same time some other user tries to write to it? That's handled differently on different platforms, and you can end up in sticky situations.

If possible, it's best to steer clear of simultaneous access problems with files on a web server using Java. There's a better way to handle the users' commentsyou store data in JSPs between page accesses using some built-in JSP 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

    Similar book on Amazon

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