JSP simple exercise-getting form data
In JSP, the most common way for a server program to interact with a client is to submit data using a form. There are two ways to submit a form: get and post. The biggest difference is that: data submitted using the get method is displayed in the address bar of the browser, but not in the post method. Therefore, the post method is more commonly used. The data submitted in the form can be a text box, a list box, and a text area. Use the getParameter () method of the request object to obtain the value of the corresponding data item in the form.
The following code is "get form data:
<% @ Page contentType = "text/html; charset = gb2312" %> <script language = "javascript"> function on_submit () // verify the validity of data {if (form1.username. value = "") {alert ("the user name cannot be blank. Enter the user name! "); Form1.username. focus (); return false;} if (form1.userpassword. value =" ") {alert (" the user password cannot be blank. Enter the password! "); Form1.userpassword. focus (); return false;} if (form1.reuserpassword. value =" ") {alert (" the password cannot be blank. Please enter the password! "); Form1.reuserpassword. focus (); return false;} if (form1.userpassword. value! = Form1.reuserpassword. value) {alert ("the password is different from the Confirmed password"); form1.userpassword. focus (); return false;} if (form1.email. value. length! = 0) {for (I = 0; I
<% @ Page contentType = "text/html; charset = gb2312" %> <%! Public String codeToString (String str) {String s = str; try {/* Note below: Do not use gb2312, use ISO-8859-1 */byte tempB [] = s. getBytes ("ISO-8859-1"); s = new String (tempB); return s ;}catch (Exception e) {return s ;}%>Receive new user registrationThis is the data submitted by the new user registration:
Username: <% = codeToString (request. getParameter ("username") %>
Password: <% = codeToString (request. getParameter ("userpassword") %>
Gender: <% = codeToString (request. getParameter ("sex") %>
Date of birth: <% = request. getParameter ("year") + request. getParameter ("month") + request. getParameter ("day") %>
Email: <% = request. getParameter ("E-mail") %>
Home address: <% = codeToString (request. getParameter ("address") %>