方法一:最基本的方法
在response.getWriter().write();的前面输入
response.setContentType("text/html;charset=GBK");
response.setContentType("text/html");
就解决了。
方法二:添加一个过滤器,再也不用担心编码的问题了.
记得在前面注解@webFilter("你要过滤的servlet"),此注解只能在version3.1可以使用,其他以下的版本要自己配置xml文件
package Filter; import java.io.IOException; 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.annotation.WebFilter;
@WebFilter("/*") public class AFilter implements Filter { //过滤所有字符编码集 @Override
public void destroy() { // TODO Auto-generated method stub } @Override public
void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); chain.doFilter(request,
response); } @Override public void init(FilterConfig arg0) throws
ServletException { // TODO Auto-generated method stub } }