服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例

2020-10-23 20:51柠檬旋风腿 Java教程

这篇文章主要介绍了Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例,非常具有实用价值,需要的朋友可以参考下

用servlet实现一个注册的小功能 ,后台获取数据。

注册页面:

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例  

注册页面代码 :

?
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/>
    爱好:<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>---请选择---</option>
         <option value="bj">北京</option>
         <option value="sh">上海</option>
         <option value="sy">沈阳</option>
        </select>   
        <br/>
    <input type="submit" value="点击注册">
  </form>
</body>
</html>

人员实体类: 注意:人员实体类要与表单中的name一致,约定要优于编码

?
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;
 
//实体类中的字段要与表单中的字段一致,约定优于编码
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页面(后台接收数据方法一)

?
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 获得填写的表单数据
 */
@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");
    //获取传过来的表单数据,根据表单中的name获取所填写的值
    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);
  }
 
}

得到的数据:

 Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例   

接收方法二:

?
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 获得填写的表单数据
 */
@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");
    //获取传过来的表单数据,根据表单中的name获取所填写的值
    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);
  }
  
  
 
}

得到的数据:

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例    

接收方法三: 利用反射赋值给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 获得填写的表单数据
 */
@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");
    //获取传过来的表单数据,根据表单中的name获取所填写的值
    
      
      try {
        user u = new user();
        system.out.println("数据封装之前: "+u);
        //获取到表单数据
        map<string, string[]> map = request.getparametermap();
        for(map.entry<string,string[]> m:map.entryset()){
          string name = m.getkey();
          string[] value = m.getvalue();
          //创建一个属性描述器
          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("封装数据之后: "+u);
      } catch (introspectionexception | illegalaccessexception | illegalargumentexception | invocationtargetexception e) {
        e.printstacktrace();
      }
      
    }
    
  
 
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }
 
}

得到的结果:

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例  

接收方法四:使用apache 的 beanutils 工具来进行封装数据(ps:这个benautils工具,struts框架就是使用这个来获取表单数据的哦!)

?
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 获得填写的表单数据
 */
@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");
    //获取传过来的表单数据,根据表单中的name获取所填写的值
  
    //方法四:使用beanutil来封装user类
    user u = new user();
    system.out.println("没有使用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);
  }
  
  
}

得到的结果:

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例   

 接收方法 方式五: 使用inputstream流来进行接收(一般字符串啥的不用这个方法,一般是文件上传下载时候才会使用这种方法)因为接收到的字符串各种乱码,编码问题解决不好

?
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 获得填写的表单数据
 */
@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");
    //获取传过来的表单数据,根据表单中的name获取所填写的值
    response.setcontenttype("text/html;charset=utf-8");
    //获取表单数据
    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);
  }
}

得到的结果:(各种乱码 。。。。)

Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

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

延伸 · 阅读

精彩推荐