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

所属分类: 软件编程 / java 阅读数: 58
收藏 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);
  }
}

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

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

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多