java web 学习笔记 编码问题总结
1.非form表单中提交的中文参数---------------------------传递给Servlet服务器时,默认以iso-8859-1解码。所以要用iso-8859-1编码,在以utf-8编码
代码示例;
String username = request.getParameter("username"); //将jsp页面用UTF-8中文文本,在服务器端默认用ISO-8859-1解码接收,然后在服务器编码用iso-8859-1编码,然后用utf-8编码解码返回。 username = new String(username.getBytes("ISO-8859-1"),"UTF-8"); From表单中传递给Servlet服务器时,如果有中文。 |
Post提交时,
request.setCharacterEncoding(“utf-8”); response.setContentType("text/html;charset=utf-8"); 如上语句有两个功能,一是:告诉服务器用utf-8编码中文。二是告诉浏览器默认用utf-8接收中文。 |
Form 表单中以Get提交(以上代码已经不起作用)浏览器默认在地址框中用iso-8859-1解码
具体处理方式如下:
解决方式:
String name = request.getParameter(“name”); Name = new String(“iso-8859-1”,”utf-8”);解决 |
过滤器 -----全站中文乱码的解决
package com.itheima.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; public class SetAllCharacterEncodingFilter implements Filter { FilterConfig filterconfig; public void init(FilterConfig filterconfig) throws ServletException { this.filterconfig = filterconfig; } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request; HttpServletResponse response; try { request = (HttpServletRequest) req; response = (HttpServletResponse) resp; } catch (ClassCastException e) { throw new ServletException("non-HTTP request or response"); } String value = filterconfig.getInitParameter("encoding"); if(value==null) { value = "UTF-8"; } request.setCharacterEncoding(value);//POST请求参数中文 response.setContentType("text/html;charset="+value); MyHttpServletRequest myrequest = new MyHttpServletRequest(request); chain.doFilter(myrequest, response); } public void destroy() { } } class MyHttpServletRequest extends HttpServletRequestWrapper { public MyHttpServletRequest(HttpServletRequest request) { super(request); } public String getParameter(String name) { String method = super.getMethod(); String value = super.getParameter(name); if("get".equalsIgnoreCase(method)) { try { value = new String(value.getBytes("ISO-8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return value; } } package com.itheima.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); response.getWriter().write("get方式提交表单:"+name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); response.getWriter().write("post方式提交表单"+name); } } |