国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

tomcat下的jsp和servlet的字符編碼問(wèn)題

2024-09-05 00:19:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
使用filter來(lái)改變r(jià)equest的編碼

在前面的文章里面,我們討論了在tomcat下的jsp和servlet的字符編碼問(wèn)題!


知道當(dāng)沒(méi)有指定request的編碼的時(shí)候,從客戶端得到的數(shù)據(jù)是iso-8859-1編碼的(request.getparameter()得到傳遞的參數(shù)值);


但是我們?cè)趺磥?lái)改變r(jià)equest的編碼呢?


方法有很多種!


 比如:在getrequestdispatcher("/jsp/jsptoserv/hello.jsp").forward(request, response);之前修改


request的編碼,那么在jsp/jsptoserv/hello.jsp中得到的參數(shù)值就是制定的編碼的字符。


本文我們使用filter來(lái)修改request的編碼!


 


1)首先編寫filter類:


package myfilter;

 

import java.io.ioexception;
import javax.servlet.*;

 

public class changecharsetfilter implements filter {

 

    protected string encoding = null;/////要制定的編碼,在web.xml中配置

    protected filterconfig filterconfig = null;


        public void destroy() {


        this.encoding = null;
        this.filterconfig = null;


    }


    public void dofilter(servletrequest request, servletresponse response, filterchain chain)
 throws ioexception, servletexception {


            if (request.getcharacterencoding() == null){
            string encoding = getencoding();////得到指定的編碼名字
            if (encoding != null)
                request.setcharacterencoding(encoding);////設(shè)置request的編碼
        }


         chain.dofilter(request, response);///有機(jī)會(huì)執(zhí)行下一個(gè)filter


    }


    public void init(filterconfig filterconfig) throws servletexception {


          this.filterconfig = filterconfig;
        this.encoding = filterconfig.getinitparameter("encoding");///得到在web.xml中配置的編碼
      }

 

    protected string getencoding() {


        return (this.encoding);///得到指定的編碼


    }

 

}

 

2。編輯web.xml文件


<?xml version="1.0" encoding="iso-8859-1"?>


<!doctype web-app
 public "-//sun microsystems, inc.//dtd web application 2.3//en"
 "http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>


<filter>
        <filter-name>setcharacterencoding</filter-name>
        <filter-class>myfilter.changecharsetfilter </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>gb2312</param-value>//////指定編碼為gb2312
        </init-param>
     </filter>
    <filter-mapping>
        <filter-name>setcharacterencoding</filter-name>
        <url-pattern>/*</url-pattern>////////對(duì)于所有的request改變其編碼
    </filter-mapping>


</web-app>
///


3。


寫一個(gè)a.jsp


<%@ page contenttype="text/html; charset=gb2312" %>
<html>
<head></head>
<body>
<%
string name=request.getparameter("name");///本來(lái)這里得到字符是iso-8859-1編碼的,不能直接


在console中輸出的,但是現(xiàn)在改變了request的編碼方式,此時(shí)的name的編碼是gb2312,所以能正確在console中顯示的。

system.out.println(name);


%>
<form action="a.jsp" method="post">
<input type="text" name="name">
<input type="submit">
</form>
<%=name%>
</body>
</html>


完!


關(guān)于中文處理的問(wèn)題就寫這些了!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浦江县| 郎溪县| 尉犁县| 南丰县| 江西省| 吴旗县| 离岛区| 抚松县| 江阴市| 巫溪县| 军事| 苍南县| 闻喜县| 惠来县| 济南市| 兴国县| 上饶市| 亚东县| 巴东县| 晋中市| 上高县| 咸宁市| 乌鲁木齐县| 富平县| 金秀| 漳州市| 新平| 青海省| 阿拉尔市| 清丰县| 宣汉县| 孝昌县| 常宁市| 文登市| 遂昌县| 隆昌县| 都兰县| 曲靖市| 甘泉县| 临潭县| 陕西省|