日期:2014-05-16  浏览次数:20351 次

服务器下访问jsp页面乱码
<!--[diy=diycontenttop]--><!--[/diy]-->
<!-- -->

?

JSP中文乱码问题之一:调用JSP页面显示乱码

通过浏览器调用JSP页面,在客户端浏览器中所有的中文内容出现乱码。

解决:

首先确认本JSP在编辑器中保存时,使用的是GBK的编码格式,然后在JSP页面的开始部分添加 < %@ pageEncoding="GBK" %>就可以解决中文乱码问题。

JSP中文乱码问题之二:调用Servlet页面显示乱码

通过浏览器调用Servlet,Servlet在浏览器中显示内容出现乱码

解决:

在Servlet使用response在输出内容之前,先执行response.setContentType("text/html;charset=GBK")设定输出内容的编码为GBK

JSP中文乱码问题之三:Post表单传递参数乱码

通过JSP页面、HTML页面或者Servlet中的表单元素提交参数给对应的JSP页面或者Servelt而JSP页面或者Servlet接收的中文参数值乱码。

解决:

在接收POST提交的参数之前,使用request.setCharacterEncoding("GBK")设定接收参数的内容使用GBK编码

JSP中文乱码问题更好的解决方法是使用过滤器技术

Encoding过滤器

  1. package?com.htt; ?
  2. ?
  3. import?java.io.IOException; ?
  4. ?
  5. import?javax.servlet.Filter; ?
  6. ?
  7. import?javax.servlet.FilterChain; ?
  8. ?
  9. import?javax.servlet.FilterConfig; ?
  10. ?
  11. import?javax.servlet.ServletException; ?
  12. ?
  13. import?javax.servlet.ServletRequest; ?
  14. ?
  15. import?javax.servlet.ServletResponse; ?
  16. ?
  17. public?class?Encoding?implements?Filter?{ ?
  18. ?
  19. ????public?void?destroy()?{??} ?
  20. ?
  21. ????public?void?doFilter(ServletRequest?request,?ServletResponse?response,???FilterChain?chain)?throws?IOException,?ServletException?{ ?
  22. ?
  23. ????request.setCharacterEncoding("GBK"); ?
  24. ?
  25. ????chain.doFilter(request,?response); ?
  26. ?
  27. ????} ?
  28. ?
  29. ????public?void?init(FilterConfig?filterConfig)?throws?ServletException?{?} ?
  30. ?
  31. }????? ?

Web.xml文件中的设置

  1. <?filter>?
  2. ?
  3. ???<?filter-name>encoding<?/filter-name><