getpost请求解决中文乱码问题

get请求正确书写中文

  • 以一个登陆界面为例
package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username="+username+"==password="+password);//这个是没有转嘛之前的
        //get请求在地址栏上已经编过码了,所以我们收到的就是乱码
        //tomcat收到这批数据getParamter默认使用的就是ISO-8859-1去解码
        //先让文字回到ISO-8859-1对应的自己数组然后按utf-8组拼字符串
        username=new String(username.getBytes("ISO-8859-1"),"utf-8");
        System.out.println("username="+username+"==password="+password);
    }
}

因为在地址栏上已经转码了,所以再接收的时候需要再转一次,就是负负为正的意思,默认的是ISO-8859-1,想要看见中文就要转为utf-8

  1. 登陆界面也给大家放在这里
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h2>请输入以下内容,完成登录</h2>
<form action="/LoginServlet2" method="get">
    账号<input type="text" name="username"><br>
    密码<input type="text" name="password"><br>
    <input type="submit" value="登录"/>
</form>
</body>
</html>
  • 解决post请求中文乱码问题
  • `package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class LoginServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //qingqiu(request);
    //post请求处理方法
    request.setCharacterEncoding("UTF-8");
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    System.out.println("post:username="+username+"==password="+password);
}

private void qingqiu(HttpServletRequest request) throws UnsupportedEncodingException {
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    System.out.println("username="+username+"==password="+password);//这个是没有转嘛之前的
    //get请求在地址栏上已经编过码了,所以我们收到的就是乱码
    //tomcat收到这批数据getParamter默认使用的就是ISO-8859-1去解码
    //先让文字回到ISO-8859-1对应的自己数组然后按utf-8组拼字符串
    username=new String(username.getBytes("ISO-8859-1"),"utf-8");
    System.out.println("username="+username+"==password="+password);
}

}
`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值