Servlet Implementation Form submission (register small function), background get form data

Source: Internet
Author: User

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

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.