Javaweb接收表单数据并处理中文乱码

所属分类: 软件编程 / java 阅读数: 50
收藏 0 赞 0 分享

前端表单数据

常见的表单项的传值,如:

  • 普通input
  • 单选radio
  • 多选checkbox
  • select下拉选择
  • textarea文本域

普通 input : name属性值为后台接收时的参数值。

用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>

单选 radio :单选按钮的 name 值相同才能实现只能点击一个。

性别:

<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女

多选checkbox :name值相同。

爱好:

<input type="checkbox" name="hobby" value="唱">唱
<input type="checkbox" name="hobby" value="跳舞">跳舞
<input type="checkbox" name="hobby" value="rap">rap
<input type="checkbox" name="hobby" value="篮球">篮球

select下拉选择 :后台通过degree作为参数,获取选中的那个option的value值。

下拉选择:

<select name="degree">
<option value="">---请选择---</option>
<option value="大一">大一</option>
<option value="大二">大二</option>
<option value="大三">大三</option>
<option value="大四">大四</option>
</select>

textarea文本域 :rows定义显示的行数,cols定义的是显示的列数。

文本域:<br><textarea name="other" rows="10" cols="30"></textarea><br>

后台接收数据

接收表单数据:

String 表单name= request.getParameter(表单name);

普通input、单选radio、select下拉选择、textarea文本域可通过此方法获取。

String[] hobbies = request.getParameterValues("hobby");

多选checkbox可通过此方法获取。

中文乱码处理

GET方式提交的数据

先通过 String username = request.getParameter(username) 获得该表单的值,此时是乱码的。

使用String new_username = new String(username.getBytes("iso8859-1"), "utf-8") 进行编码转换

相关APi :

String(byte[] bytes, Charset charset) 构造一个新的String,由指定的字节的数组转化为指定编码的字节数组。

getBytes(Charset charset)使用指定的编码方式将该String编码为字节序列,将结果存储到新的字节数组中。

解释:通过get方式提交的数据的编码方式为iso8859-1, 先获取该编码方式的字节数组,再将该字节数组转化为utf-8编码的字节数组,然后将该字节数组转换为字符串。

POST方式提交的数据

request.setCharacterEncoding("utf-8");

服务器端向客户端发送的数据

response.setContentType("text/html;charset=utf-8");

以下是全部代码:

GET提交方式:

@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //get提交方式处理中文乱码
    String username = request.getParameter("username");
    String new_username = new String(username.getBytes("iso8859-1"), "utf-8");
    
    String password = request.getParameter("password");
    String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
    
    String gender = request.getParameter("gender");
    String new_gender = new String(gender.getBytes("iso8859-1"), "utf-8");
    
    String[] hobbies = request.getParameterValues("hobby");
    for (int i = 0; i < hobbies.length; i++) {
      hobbies[i]=new String(hobbies[i].getBytes("iso8859-1"), "utf-8");
    }
    
    String degree = request.getParameter("degree");
    String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
    
    String other = request.getParameter("other");
    String new_password = new String(password.getBytes("iso8859-1"), "utf-8");
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }
}

POST提交方式:

@WebServlet(name = "RegisterServlet",urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //post提交方式的中文乱码解决方法
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String gender = request.getParameter("gender");
    String[] hobbies = request.getParameterValues("hobby");
    String degree = request.getParameter("degree");
    String other = request.getParameter("other");
    
    //如果服务器端需要向客户端发送的数据
    response.setContentType("text/html;charset=utf-8");
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }
}

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

更多精彩内容其他人还在看

Java输入输出流复制文件所用时间对比

这篇文章主要介绍了Java输入输出流复制文件所用时间对比的相关资料,非常不错,具有参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程中start和run方法全面解析

这篇文章主要介绍了Java线程中start和run方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的JSON处理器fastjson使用方法详解

下面小编就为大家带来一篇Java的JSON处理器fastjson使用方法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java 二维码,QR码,J4L-QRCode 的资料整理

本文主要介绍Java 中二维码,QR码,J4L-QRCode,这里整理了详细的资料供大家学习参考关于二维码的知识,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

java哈夫曼树实例代码

这篇文章主要为大家介绍了java哈夫曼树实例代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android读取本地或网络图片并转换为Bitmap

这篇文章主要为大家详细介绍了Android读取本地或网络图片,并转换为Bitmap,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java日期时间操作的方法

这篇文章主要为大家详细介绍了Java日期时间操作的一些方法,获得Calendar,定义日期/时间的格式等,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 获取路径的各种方法(总结)

下面小编就为大家带来一篇java 获取路径的各种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java数据结构与算法之奇偶排序算法完整示例

这篇文章主要介绍了java数据结构与算法之奇偶排序算法,较为详细的分析了奇偶算法的原理并结合完整示例形式给出了实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

java数据结构与算法之双向循环队列的数组实现方法

这篇文章主要介绍了java数据结构与算法之双向循环队列的数组实现方法,结合实例形式分析了双向循环队列的原理与数组实现技巧,并附带说明了该算法的用途,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多