温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-23 09:16:23 来源:亿速云 阅读:288 作者:小猪 栏目:开发技术

这篇文章主要讲解了Javaweb接收表单数据并处理中文乱码的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

前端表单数据

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

  • 普通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);
  }
}

看完上述内容,是不是对Javaweb接收表单数据并处理中文乱码的方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI