mysql+javaWeb+jdbc+tomcat開發中的中文亂碼處理


1.修改工作空間編碼方式為UTF-8

  • window-->properties-->General-->Workspace-->Other-->UTF-8 修改項目為UTF-8

2.創建的jsp全部修改為UTF-8

  • 修改jsp默認編碼方式 window --> preference --> Web --> JSP Files --> Encoding 修改為UTF-8 修改jsp默認編碼為UTF-8

3.在doGet()/doPost里面加上以下代碼

request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); 

4.對於get方式提交到Servlet的數據,中文亂碼處理請參照下面示例

String username = request.getParameter("username"); username = new String(username.getBytes("ISO-8859-1"),"UTF-8"); 

5.mysql亂碼處理,在原來的URL后面加上?useUnicode=true&characterEncoding=UTF-8

jdbcUrl=jdbc:mysql://localhos:3306/greadManagert?useUnicode=true&characterEncoding=UTF-8 
  • 如果在xml中識別不了&符號,要使用實體字符"&amp"加“;”來代替&,即
jdbcUrl=jdbc:mysql://localhos:3306/greadManagert?useUnicode=true&characterEncoding=UTF-8 

6.修改tomcat配置文件server.xml,63行,加上URIEncoding="utf-8"

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> 

7.配置亂碼過濾器

 <!-- 亂碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

番外篇:自動提示

  • window-->preference-->java-->Editor-->Content Assist-->Auto activation triggers for java,在里面把a--z二十六個字母全輸入進去

eclipse自動提示設置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM