其他分享
首页 > 其他分享> > Servlet 解决乱码

Servlet 解决乱码

作者:互联网

package com.xxxx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/s03")
public class Servlet3 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 解决乱码 改变编码方式
         *  GET
         *  POST
         */
        //只针对post
        req.setCharacterEncoding("UTF-8");
        //对get  tomcat8以上不会乱码  以下
        String name=new String(req.getParameter("uname").getBytes("ISO-8859-1"),"UTF-8");


        String uname=req.getParameter("uname");
        String upwd=req.getParameter("upwd");
        System.out.println(uname+" "+upwd);
    }
}

标签:String,servlet,req,乱码,uname,解决,import,Servlet,javax
来源: https://www.cnblogs.com/lwx11111/p/16103523.html