MyEclipse開發的Web項目編碼問題解決

來源:互聯網
上載者:User

標籤:general   開發   update   項目   file   

一、開發工具中可以修改編碼的地方:

    1)Window --> Preferences --> General --> Content Types --> Text

    選擇要設定的檔案,在Default encoding 中輸入【GBK或者UTF-8】,點擊後面的update

    2)Window --> Preferences --> Workspace --> Text file encoding

    在Other後面的選擇框中輸入【GBK或者UTF-8】,點擊Applay和OK

    3)右鍵點擊項目、檔案。選擇Properties --> Text file encoding

    同樣在Other後面的選擇框中輸入【GBK或者UTF-8】,點擊Applay和OK


二、檔案中可以修改編碼的地方:

    僅限於jsp檔案和html檔案等聲明部分有編碼設定的檔案,

    1)<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

    2)<meta http-equiv="Content-Type" content="text/html; charset=GBK" />


三、用過濾器實現編碼過濾

    1)過濾器處理類

    建立一個實現了Filter介面的java類

    

package com.hchx.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;/** *  * @ClassName: CharacterFilter * @Description: |-- 編碼過濾器的實作類別 * @author GNODIAD * @date 2014-11-13 上午09:27:50 */public class CharacterFilter implements Filter{// 聲明代碼處理參數private String srcCharset;private String targetCharset;public void destroy() {// TODO 待實現方法}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {System.out.println("開始進行編碼過濾...");// 在控制台列印提示資訊request.setCharacterEncoding(srcCharset);// 佈建要求編碼response.setCharacterEncoding(targetCharset);// 設定響應編碼chain.doFilter(request, response); // 向後傳遞}public void init(FilterConfig config) throws ServletException {srcCharset = config.getInitParameter("srcCharSet");targetCharset = config.getInitParameter("targetCharSet");}}

    2)在web.xml中配置過濾器

    

<filter><filter-name>charset</filter-name><filter-class>com.hchx.filter.CharacterFilter</filter-class><init-param><param-name>srcCharSet</param-name><param-value>GBK</param-value></init-param><init-param><param-name>targetCharSet</param-name><param-value>GBK</param-value></init-param></filter><filter-mapping><filter-name>charset</filter-name><url-pattern>/*</url-pattern></filter-mapping>

小結:解決編碼問題,從這三個方面入手,具體操作因個人開發的項目而異。

本文出自 “GNODIAD” 部落格,請務必保留此出處http://gnodiad.blog.51cto.com/9569162/1575961

MyEclipse開發的Web項目編碼問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.