servlet乱码以及解决

时间:2022-05-24 06:14:09

//  浏览器提交的数据是000110011(码表中对应的《编码》 )等东西。

// 浏览器以什么《码表》打开浏览器(而空中浏览器使用的编码是:<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 这条语句控制的),那么就以什么《码表》提交数据。


    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1(request, response); }
    //get提交时候的乱码处理
private void test1(HttpServletRequest request, HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
response.getWriter().write(request.getCharacterEncoding()+"<br/>");
//得到以get类型提交的数据,由于设置request.setCharacterEncoding("UTF-8")对get类型的无效
String username=request.getParameter("username");
username=new String(username.getBytes("iso8859-1"),"UTF-8");
response.getWriter().write(username);
}

我用的是tomcat服务器,tomcat默认使用的码表ISO8889

servlet乱码以及解决

也可以设服务器的conf下的server.xml文件(不过这个一般不推荐使用!)

    服务器下的server.xml 文件的设置:

如: <Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443" URIEncoding="UTF-8" />

另外一种设置(这样就表示使用post方式中的编码):

<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"  useBodyEncodingForURI="true"/>

假如使用这种形式:那么request.setCharacterEncoding("UTF-8");也对get类型提交的数据有效

也就是下面这种设置:

  

    //处理浏览器提交的数据《post》类型的
private void test(HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException, IOException {
//这种只能处理《post提交的数据有效,对get提交的数据没有用》
request.setCharacterEncoding("UTF-8");//假如提交的时候用的是《UTF-8》的编码,那么在这里使用
//UTF-8来解析 request.getParameter("username");在这里的时候就会以UTF-8来解析了 response.setCharacterEncoding("UTF-8");//由于response.getWriter()返回的字符流,所以需要设置输出的
response.setHeader("Content-type", "text/html;charset=UTF-8");//这个高速浏览器以什么编码解析html文件
String username=request.getParameter("username");
String password=request.getParameter("userpassWord");
response.getWriter().write(username);
response.getWriter().write(password);
}

如有错误,欢迎指点

最终版本,使用filer解决全站乱码

public class CharacterEncodingFilter implements Filter {
private FilterConfig config=null;
private String defaultCharset="UTF-8";//缺省的编码
public void init(FilterConfig filterConfig) throws ServletException {
this.config=filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp; String charset=this.config.getInitParameter("charset");
if(charset==null||"".equals(charset)){
charset=defaultCharset;
} request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset); CharacterEncodingRequest requestWrapper=new CharacterEncodingRequest(request);
chain.doFilter(requestWrapper, response); }
public void destroy() { } } class CharacterEncodingRequest extends HttpServletRequestWrapper{
private HttpServletRequest requst;
public CharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.requst=request;
} @Override
public String getParameter(String name) {
try {
String value=this.getParameter(name);
if(value==null)return null;
if(!this.requst.getMethod().equalsIgnoreCase("get"))return value;//忽略大小写 value=new String (value.getBytes("ISO8859-1"),this.requst.getCharacterEncoding());//这里是没有更改tomcat默认编码的时候
return value;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}

xml文件

    <filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>cn.my.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>