Using a servlet to implement a registered small function, the background to obtain data.
Registration page:
Registration page Code:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Insert Title here</title></Head><Body> <formAction= "/requestdemo/requestdemo3"Method= "POST">User name:<inputtype= "text"name= "UserName"><BR/>Password:<inputtype= "text"name= "pwd"><BR/>Gender:<inputtype= "Radio"name= "Sex"value= "Male"checked= "Checked">male<inputtype= "Radio"name= "Sex"value= "female">Woman<BR/>Hobbies:<inputtype= "checkbox"name= "Hobby"value= "Football">Football<inputtype= "checkbox"name= "Hobby"value= "Basketball">Basketball<inputtype= "checkbox"name= "Hobby"value= "Volleyball">Volleyball<inputtype= "checkbox"name= "Hobby"value= "Badminton">Badminton<BR/>Your city:<Selectname= "City"> <option>---Please select---</option> <optionvalue= "BJ">Beijing</option> <optionvalue= "sh">Shanghai</option> <optionvalue= "Sy">Shenyang</option> </Select> <BR/> <inputtype= "Submit"value= "Click to register"> </form></Body></HTML>
People entity class: Note: The person entity class is consistent with the name in the form, and the Convention is better than the encoding
PackageCom.chensi.bean;//The fields in the entity class are consistent with the fields in the form, and the conventions are better than the encodings Public classUser {PrivateString UserName; PrivateString pwd; PrivateString sex; Privatestring[] hobby; PrivateString City; PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } Publicstring[] Gethobby () {returnHobby; } Public voidsethobby (string[] hobby) { This. Hobby =Hobby; } PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } }
Servlet page (background receive data method one):
PackageCom.chensi;Importjava.io.IOException;ImportJava.util.Iterator;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet gets completed form data*/@WebServlet ("/requestdemo3") Public classRequestDemo3extendsHttpServlet {Private Static Final LongSerialversionuid = 1L; protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); //get the form data passed in, get the values you've filled out based on the name in the formString userName = Request.getparameter ("UserName"); String pwd= Request.getparameter ("pwd"); String Sex= Request.getparameter ("Sex"); String[] Hobbys= Request.getparametervalues ("Hobby"); System.out.println (UserName); System.out.println (PWD); SYSTEM.OUT.PRINTLN (Sex); for(inti = 0; hobbys!=NULL&&i < Hobbys.length; i++) {System.out.println (Hobbys[i]+ "\ T"); } } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
The data obtained:
Servlet Implementation Form submission (register small function), background get form data