Filter 顾名思义,就是过滤器,运行项目时会先加载Filter过滤器。
解决servlets在浏览器上的乱码用到的代码是
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
response.setContentType("text/html;charset=UTF-8");
为了一劳永逸,不必在每个servlets都写上这俩行代码,可以创建一个Filter,在里面加入这个代码。
之后还要在web.xml中配置如下代码
<filter>
<display-name>ServletsFilter</display-name>
<filter-name>ServletsFilter</filter-name>
<filter-class>com.Filter.ServletsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ServletsFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<display-name>ServletsFilter</display-name>
<filter-name>ServletsFilter</filter-name>
<filter-class>com.Filter.ServletsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ServletsFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>