JSP simple exercise-getting form data

Source: Internet
Author: User

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") %>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.