一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例

2020-10-23 20:51檸檬旋風(fēng)腿 Java教程

這篇文章主要介紹了Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

用servlet實(shí)現(xiàn)一個(gè)注冊(cè)的小功能 ,后臺(tái)獲取數(shù)據(jù)。

注冊(cè)頁(yè)面:

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例  

注冊(cè)頁(yè)面代碼 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>insert title here</title>
</head>
<body>
  <form action="/requestdemo/requestdemo3" method="post">
    用戶名:<input type="text" name="username"><br/>
    密碼:<input type="text" name="pwd"><br/>
    性別:<input type="radio" name="sex" value="男" checked="checked">男
      <input type="radio" name="sex" value="女">女<br/>
    愛(ài)好:<input type="checkbox" name="hobby" value="足球">足球
      <input type="checkbox" name="hobby" value="籃球">籃球
      <input type="checkbox" name="hobby" value="排球">排球
      <input type="checkbox" name="hobby" value="羽毛球">羽毛球<br/>
    所在城市:<select name="city">
         <option>---請(qǐng)選擇---</option>
         <option value="bj">北京</option>
         <option value="sh">上海</option>
         <option value="sy">沈陽(yáng)</option>
        </select>   
        <br/>
    <input type="submit" value="點(diǎn)擊注冊(cè)">
  </form>
</body>
</html>

人員實(shí)體類: 注意:人員實(shí)體類要與表單中的name一致,約定要優(yōu)于編碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.chensi.bean;
 
//實(shí)體類中的字段要與表單中的字段一致,約定優(yōu)于編碼
public class user {
 
  private string username;
  private string pwd;
  private string sex;
  private string[] hobby;
  private string city;
  public string getusername() {
    return username;
  }
  public void setusername(string username) {
    this.username = username;
  }
  public string getpwd() {
    return pwd;
  }
  public void setpwd(string pwd) {
    this.pwd = pwd;
  }
  public string getsex() {
    return sex;
  }
  public void setsex(string sex) {
    this.sex = sex;
  }
  public string[] gethobby() {
    return hobby;
  }
  public void sethobby(string[] hobby) {
    this.hobby = hobby;
  }
  public string getcity() {
    return city;
  }
  public void setcity(string city) {
    this.city = city;
  }
  
}

接收方法一:         servlet頁(yè)面(后臺(tái)接收數(shù)據(jù)方法一)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.chensi;
 
import java.io.ioexception;
import java.util.iterator;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
/**
 * servlet 獲得填寫(xiě)的表單數(shù)據(jù)
 */
@webservlet("/requestdemo3")
public class requestdemo3 extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值
    string 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 (int i = 0; hobbys!=null&&i < hobbys.length; i++) {
      system.out.println(hobbys[i]+"\t");
    }
  }
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
 
}

得到的數(shù)據(jù):

 Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例   

接收方法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.chensi;
 
import java.io.ioexception;
import java.util.enumeration;
import java.util.iterator;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
/**
 * servlet 獲得填寫(xiě)的表單數(shù)據(jù)
 */
@webservlet("/requestdemo3")
public class requestdemo3 extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值
    enumeration<string> names = request.getparameternames();
    while (names.hasmoreelements()) {
      string strings = (string) names.nextelement();
      string[] parametervalues = request.getparametervalues(strings);
      for (int i = 0;parametervalues!=null&&i < parametervalues.length; i++) {
        system.out.println(strings+":"+parametervalues[i]+"\t");
      }
    }
  }
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
  
  
 
}

得到的數(shù)據(jù):

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例    

接收方法三: 利用反射賦值給user

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.chensi;
 
import java.beans.introspectionexception;
import java.beans.propertydescriptor;
import java.io.ioexception;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.enumeration;
import java.util.iterator;
import java.util.map;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.chensi.bean.user;
 
/**
 * servlet 獲得填寫(xiě)的表單數(shù)據(jù)
 */
@webservlet("/requestdemo3")
public class requestdemo3 extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值
    
      
      try {
        user u = new user();
        system.out.println("數(shù)據(jù)封裝之前: "+u);
        //獲取到表單數(shù)據(jù)
        map<string, string[]> map = request.getparametermap();
        for(map.entry<string,string[]> m:map.entryset()){
          string name = m.getkey();
          string[] value = m.getvalue();
          //創(chuàng)建一個(gè)屬性描述器
          propertydescriptor pd = new propertydescriptor(name, user.class);
          //得到setter屬性
          method setter = pd.getwritemethod();
          if(value.length==1){
            setter.invoke(u, value[0]);
          }else{
            setter.invoke(u, (object)value);
          }
        }
        system.out.println("封裝數(shù)據(jù)之后: "+u);
      } catch (introspectionexception | illegalaccessexception | illegalargumentexception | invocationtargetexception e) {
        e.printstacktrace();
      }
      
    }
    
  
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
 
}

得到的結(jié)果:

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例  

接收方法四:使用apache 的 beanutils 工具來(lái)進(jìn)行封裝數(shù)據(jù)(ps:這個(gè)benautils工具,struts框架就是使用這個(gè)來(lái)獲取表單數(shù)據(jù)的哦!)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.chensi;
 
import java.beans.introspectionexception;
import java.beans.propertydescriptor;
import java.io.ioexception;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.enumeration;
import java.util.iterator;
import java.util.map;
 
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import org.apache.commons.beanutils.beanutils;
 
import com.chensi.bean.user;
 
/**
 * servlet 獲得填寫(xiě)的表單數(shù)據(jù)
 */
@webservlet("/requestdemo3")
public class requestdemo3 extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值
  
    //方法四:使用beanutil來(lái)封裝user類
    user u = new user();
    system.out.println("沒(méi)有使用beanutil封裝之前: "+u);
    try {
      beanutils.populate(u, request.getparametermap());
      system.out.println("使用beanutils封裝之后: "+u);
    } catch (illegalaccessexception | invocationtargetexception e) {
      e.printstacktrace();
    }
      
    }
    
  
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
  
  
}

得到的結(jié)果:

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例   

 接收方法 方式五: 使用inputstream流來(lái)進(jìn)行接收(一般字符串啥的不用這個(gè)方法,一般是文件上傳下載時(shí)候才會(huì)使用這種方法)因?yàn)榻邮盏降淖址鞣N亂碼,編碼問(wèn)題解決不好

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.chensi;
 
import java.beans.introspectionexception;
import java.beans.propertydescriptor;
import java.io.ioexception;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.enumeration;
import java.util.iterator;
import java.util.map;
 
import javax.servlet.servletexception;
import javax.servlet.servletinputstream;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import org.apache.commons.beanutils.beanutils;
 
import com.chensi.bean.user;
 
/**
 * servlet 獲得填寫(xiě)的表單數(shù)據(jù)
 */
@webservlet("/requestdemo3")
public class requestdemo3 extends httpservlet {
  private static final long serialversionuid = 1l;
    
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值
    response.setcontenttype("text/html;charset=utf-8");
    //獲取表單數(shù)據(jù)
    servletinputstream sis = request.getinputstream();
    int len = 0;
    byte[] b = new byte[1024];
    while((len=sis.read(b))!=-1){
      system.out.println(new string(b, 0, len, "utf-8"));
    }
    
    sis.close();
    
  }
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
}

得到的結(jié)果:(各種亂碼 。。。。)

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊(cè)小功能),后臺(tái)獲取表單數(shù)據(jù)實(shí)例

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/zhanghaoliang/p/5622900.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 果冻传媒天美传媒在线小视频播放 | 国产麻豆精品免费视频 | 国内外精品免费视频 | 亚洲精品www久久久久久久软件 | www国产91| 深夜视频在线播放 | 逼逼爱| 国产欧美成人不卡视频 | 国产午夜精品福利 | yy8090韩国日本三理论免费 | 手机看片国产免费现在观看 | 91寡妇天天综合久久影院 | 免费稚嫩福利 | 日本五级床片全都免费播放 | 大伊香蕉在线精品不卡视频 | 日韩欧美在线看 | 麻豆视频网 | 亚洲第一区在线观看 | 国内精品91最新在线观看 | 亚洲 另类 欧美 变态屎尿 | darkside动漫在线观看 | 日本精品久久久久久久久免费 | 91久久夜色精品国产九色 | 成年视频在线播放 | 日本高清有码视频 | 天堂网www在线中文天堂 | 国产福利视频一区二区微拍视频 | 狠狠色成人综合网图片区 | 短篇最污的乱淫伦小说全集 | 日本漫画工囗全彩番在线 | 亚洲黄色高清 | 不良研究所地址一 | 被老外玩爽的中国美女视频 | 波多野给衣一区二区三区 | 亚洲午夜性春猛交xxxx | 甜蜜惩罚小说 | 国产性片在线观看 | 操穴勤| 免费国产福利 | 91这里只有精品 | 久久影院中文字幕 |