JSP simple exercise-Get form data with Servlet

Source: Internet
Author: User

JSP simple exercise-Get form data with Servlet

// JavaBean code package servlet; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class AcceptUserRegist extends HttpServlet {public String codeToString (String str) // function for processing Chinese strings {String s = str; try {byte tempB [] = s. getBytes ("ISO-8859-1"); s = new String (tempB); return s;} catch (Exception e) {return s ;}} public void init (ServletConfig config) throws ServletException {super. init (config);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// set mimeresponse. setContentType ("text/html; charset = GB2312"); PrintWriter out = response. getWriter (); out. println ("Receive new user registration "); Out. println (" this is the data submitted by the new user registration:
"); Out. println (" username: "+ codeToString (request. getParameter (" username ") +"
"); Out. println (" Password: "+ codeToString (request. getParameter (" userpassword ") +"
"); Out. println (" Gender: "+ codeToString (request. getParameter (" sex ") +"
"); Out. println ("Date of Birth:" + request. getParameter ("year") + request. getParameter ("month") + request. getParameter ("day") +"
"); Out. println (" Email: "+ request. getParameter (" E-mail ") +"
"); Out. println (" Home address: "+ codeToString (request. getParameter (" address ") +"
"); Out. print (" ");}}

 <% @ 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
In addition And Add the following Configuration:

  
      
  
   acceptUserRegist
       
  
   servlet.AcceptUserRegist
    
   
      
  
   acceptUserRegist
       
  
   /acceptUserRegist
    
 
Enter the URL: localhost: 8080/jspdemo/servletform. jsp

Enter information ~ As follows:

After submission, the following page is displayed:



Related Article

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.