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