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: