package com.zl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//request编码问题
public class RequestDemo4 extends HttpServlet
{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        //手工处理request的get方式乱码
        
        //1把传过来的数据,以原来的编码方式反编码,
        //req.setCharacterEncoding("UTF-8")这种无效对于get方式
        //例如原来的数据是 开心,UTF-8编码,这个是浏览器编码,编码成 98 99,服务器的request是ISO8859-1把这两个数据编码成了& *
        //2& *传过来,我们用ISO8859-1编码成 98 99,再用UTF-8编成 开心
        
        String name = req.getParameter("username");
        new String(name.getBytes("is8859-1"),"utf-8");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        //这种方式只对post方式有效
        //例如原来的数据是 开心,UTF-8编码,这个是浏览器编码,编码成 98 99,服务器的request是ISO8859-1 把这两个数据编码成了& *
        //设置request的编码编成utf-8这样 服务器的REQUEST还是用UTF-8编码,还是98 99,我们直接接收就行
        req.setCharacterEncoding("UTF-8");//设置request的编码
        String name = req.getParameter("username");
        System.out.println(name);
    }

}

相关文章:

  • 2021-09-23
  • 2022-12-23
  • 2021-09-23
  • 2021-09-15
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
猜你喜欢
  • 2021-11-14
  • 2021-11-14
  • 2022-03-08
相关资源
相似解决方案