ServletJsp中解决乱码问题

来源:互联网 发布:在中文windows 编辑:程序博客网 时间:2024/04/30 14:20

在post方法中,只需要设置:request.setCharacterEncoding("UTF-8");

在get方法里面可以这样:

String username=request.getParameter("username");

String username=new String(username.getBytes("iso-8859-1"),"UTF-8");

System.out.println(username);



jsp中:<a href="ThreadSrevlet?name=中国" >点击</a>


String name=request.getParameter("name");

String name=new String(username.getBytes("iso-8859-1"),"UTF-8");

System.out.println(name);

在Cookie中,可以用:


Cookie cookie = new Cookie("cookie",URLEncoder.encode("您好!","UTF-8") );

Cookie[] cookies = request.getCookies();

if (cookies[i].getName().equals("cookie")) {

cookie2 = URLDecoder.decode(cookies[i].getValue(),"UTF-8");

}


原创粉丝点击