当前位置: 首页 > 编程笔记 >

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

莘钧
2023-03-14
本文向大家介绍Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例,包括了Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例的使用技巧和注意事项,需要的朋友参考一下

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

注册页面:

  

注册页面代码 :

<!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一致,约定要优于编码

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页面(后台接收数据方法一)

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);
  }

}

得到的数据:

    

接收方法二:

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);
  }
  
  

}

得到的数据:

    

接收方法三: 利用反射赋值给User

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);
  }

}

得到的结果:

  

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

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);
  }
  
  
}

得到的结果:

   

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

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);
  }
}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍AngularJS实现注册表单验证功能,包括了AngularJS实现注册表单验证功能的使用技巧和注意事项,需要的朋友参考一下 本文为大家分享了案例: 注册表单验证,供大家参考,具体内容如下 需要使用的两张图片: dui.gif:cuo.gif: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍python实现的登录与提交表单数据功能示例,包括了python实现的登录与提交表单数据功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python实现的登录与提交表单数据功能。分享给大家供大家参考,具体如下: 更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、

  • 本文向大家介绍利用AjaxSubmit()方法实现Form提交表单后回调功能,包括了利用AjaxSubmit()方法实现Form提交表单后回调功能的使用技巧和注意事项,需要的朋友参考一下 1.      背景 最近在工作中,需要实现网页端图片上传到FTP服务器的功能。上传文件是用Form表单提交数据的方法向后台传输文件流,在此遇到了一个问题:后台在处理完图片上传功能后,需要向前台回传是否上传成功的

  • 本文向大家介绍php实现表单提交上传文件功能,包括了php实现表单提交上传文件功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了php实现表单提交上传文件功能的具体代码,供大家参考,具体内容如下 首先创建含表单的html文件:upload.html 再创建服务端文件:upload.php 点击提交后呈现出文件: 本文已被整理到了《php文件上传操作汇总》 ,更多精彩内容,欢迎大家学

  • 本文向大家介绍Javaweb获取表单数据的多种方式,包括了Javaweb获取表单数据的多种方式的使用技巧和注意事项,需要的朋友参考一下 Javaweb获取表单数据的几种方式 一、通过键值对的形式获取表单数据 getParameter(String name):通过key,返回一个value。 getParameterValues(String name):通过key返回一个string数组(多个值

  • 本文向大家介绍js实现不提交表单获取单选按钮值的方法,包括了js实现不提交表单获取单选按钮值的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了js实现不提交表单获取单选按钮值的方法。分享给大家供大家参考。具体如下: 这是JS实现的特效,不提交表单获取相关的控件值。如本例所示,不提交表单即可获取单选按钮的值。 运行效果截图如下: 在线演示地址如下: http://demo.jb51.ne