JSPÒ³Ãæ´«ÖµÖÐÎÄÂÒÂëµÄ¼òµ¥½â¾ö·½·¨
Ô´´úÂ룬a.jspÍùb.jsp´«Öµ£º
a.jspÒ³ÃæÄÚÈÝ
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<form action="b.jsp">
¡¡¡¡name:<input type=text name=name>
¡¡¡¡<input type=submit>
</form>
b.jspÒ³ÃæÄÚÈÝ£º
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
String name=request.getParameter("name");
%>
<body>
¡¡¡¡<%=name%>
</body>
ÏÖÏó£ºÖÐÎÄÂÒÂë,±ÈÈç“£¿£¿£¿£¿£¿£¿”
¡¡¡¡½â¾ö·½·¨ÈçÏ£º
¡¡¡¡·½·¨Ò»£º
¡¡¡¡1. ÔÚb.jspÖаÑpageEncoding="GB2312"¸ÄΪpageEncoding="ISO8859-1"
¡¡¡¡ËäÈ»ÕâÑùb.jspÒ³ÃæÏÔʾµÄÄÚÈÝ»¹ÊÇÂÒÂ룬µ«ÊDz»ÊÇÄÇÖÖ“£¿£¿£¿£¿£¿£¿”µÄÂÒÂ룬¶øÊÇÒ»Ð©ÌØÊâ×Ö·û
¡¡¡¡2. È»ºóÔÚä¯ÀÀÆ÷Öв鿴²Ëµ¥ÖÐÐ޸ijÉGB2312µÄ±àÂ룬ÕâʱÂÒÂë¾ÍÏÔʾÖÐÎÄÁË¡£
¡¡¡¡3. µ«ÊÇÕâÖÖ·½·¨ÊDz»¿ÉÐеġ£
¡¡¡¡·½·¨¶þ£º
¡¡¡¡1. ÔÚb.jspÖаÑString name=request.getParameter("name");ÐÞ¸ÄΪ
¡¡¡¡String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"GB2312");
¡¡¡¡2. ÕâʱÔÙÔÚÒ³ÃæÉÏÏÔʾ£¬Ôò¾ÍÊÇÖÐÎÄÁË¡£
¡¡¡¡·½·¨Èý£º
¡¡¡¡1. ÓÐÈË˵ÐÞ¸Äget/postµÄ´«Öµ·½Ê½,µ«ÊÇÕâÊÇÓÐǰÌáµÄ,Èç¹ûÖ»ÊǰÑget·½Ê½Ð޸ijÉpost·½Ê½,Ò³Ãæ½á¹û»¹ÊÇÂÒÂë!
¡¡¡¡2. ǰÌáÄã±ØÐëÅäÖÃÁ˹ýÂËÆ÷,Èô¹ûÄãÖ»ÊÇÅäÖÃÁ˹ýÂËÆ÷,Ôò´«Öµ·½Ê½±ØÐëÊÇ:post·½Ê½²Å²»ÊÇÂÒÂë,Èç¹ûÊÇget·½Ê½ÕÕÑùÊÇÂÒÂë!
¡¡¡¡3. ¹ýÂËÆ÷µÄÅäÖÃ,ÎÒÏë´ó¼Ò¶¼ÄÜÓÐ,ÎҾͲ»ËµÁË
¡¡¡¡·½·¨ËÄ:
¡¡¡¡1. ÓÐÈË˵ÅäÖÃtomcatµÄÅäÖÃÎļþserver.xmlÀïÕâ¾ä£º
<Connector URIEncoding="GB2312"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ port="8080" maxHttpHeaderSize="8192"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ enableLookups="false" redirectPort="8443" acceptCount="100"
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ connectionTimeout="20000" disableUploadTimeout="true" />
¼ÓÉÏÕâ¾ä£ºURIEncoding="GB2312"
¡¡¡¡2. ËäÈ»¿ÉÒÔµ«ÊÇÓÐǰÌáµÄ,Èô¹ûÄã¹ýÂËÆ÷ûÓÐÅäÖÃ,ÔòÖ»ÊÇÅäÖÃÁËserver.xmlÎļþ,ÕâÑùÖ»ÓÐÔÚ±íµ¥ÊÇget´«ÖµÊ±ºò²Å¿ÉÒÔ!µ±ÊÇpost·½Ê½Ê±,ÕÕÑùÊÇÂÒÂë!
¡¡¡¡*ÕâʱÎÒÃǾͿÉÒÔ¿´³öÔÚtomcat5ÖеÄpostÓëget´«Öµ·½Ê½ÊDz»Ò»ÑùµÄ*
¡¡¡¡*ÓÐÈËÒªÎÊÓÐûÓÐÒ»ÖÖ·½·¨Èô«ÖµÊ¹ÓÃget·½Ê½»òpost·½Ê½¶¼ºÃÓÃÄØ,ÏÂÃæ¾ÍÊÇ
Ïà¹ØÎĵµ£º
1.request¶ÔÏó
¿Í»§¶ËµÄÇëÇóÐÅÏ¢±»·â×°ÔÚrequest¶ÔÏóÖУ¬Í¨¹ýËü²ÅÄÜÁ˽⵽¿Í»§µÄÐèÇó£¬È»ºó×ö³öÏìÓ¦¡£ËüÊÇHttpServletRequestÀàµÄʵÀý¡£
ÐòºÅ ·½ ·¨ ˵ Ã÷
1 object getAttribute(String name) ·µ»ØÖ¸¶¨ÊôÐÔµÄÊôÐÔÖµ
2 Enumeration getAttributeNames() ·µ»ØËùÓпÉÓÃÊôÐÔÃûµÄö¾Ù
3 String getCharacterEncoding( ......
½ñÌì²âÊÔÈËÔ±¸øÎÒÌáÁËÒ»¸öbug£¬ËµÎÒ¿ª·¢µÄÄ£¿é±¨¿ò¼ÜµÄjs´íÎ󣬵±Ê±Ò»Ìý¾ÍãÂÁË£¬ÎÒ±¾µØ²âÊÔÍêȫûÎÊÌ⣬ÔõôÉÏÁË·þÎñÆ÷»á³öÏÖÕâ¸öÎÊÌâÄØ£¿
ÓÚÊÇÎÒÈ¥·þÎñÆ÷ÉϲâÁËÒ»ÏÂ×Ô¼ºÐ´µÄÄǸöÄ£¿é£¬½á¹û±¨µÄ´íÎ󻹺ͲâÊÔÈËÔ±Ìá¸øÎҵĴíÎó²»Ò»Ö£¬ÔÓ»ØÊ£¿ÓÚÊÇÎÒÓÖÕÒµ½Ò»ÃûͬÊ£¬ÈÃËû°ïÎÒ²âһϣ ......
ʵÑ鱨¸æ
Ò». »ù±¾Ë¼Â·¼°ÊµÑé½á¹û£¨¼Ç¼¸÷ÖÖÔËÐÐÇé¿ö»òÒ³ÃæµÄÔËÐÐЧ¹û£©£»
1¡¢Ê¹ÓÃTOMCAT·þÎñÆ÷ÅäÖÃjspÓ¦ÓÃ
1£©´ò¿ªTOMCAT/webapps×ÓĿ¼£¬´´½¨Ò»webÓ¦Óã¨Èçmyweb£©£¬½«example0.jspÓëexample1.jspÎļþ¸´ÖÆÈëÄÚ£¬²¢ÔÚ¸ÃĿ¼ÏÂÉèÖÃWEB-INFĿ¼¼°web.xmlÅäÖÃÎļþ£»
2£©ÔÚä¯ÀÀÆ÷µØÖ·À¸ÊäÈëhttp://localhost:8080/myweb/ex ......
½«²ÎÊýд³É java.net.URLEncoder.encode(sContent,"gb2312")ºóÔÙ´«µÝ¡£
±ÈÈçurlÊÇ"/accept.do?content=<%=java.net.URLEncoder.encode(sContent,"UTF-8")%>";
¶Áȡʱ£¬Ê¹ÓÃ
String sc = new String(request.getParameter("content").getBytes("iso-8859-1"),"UTF-8"); ......
1. jspÒ³ÃæÏÔʾÖÐÎÄÂÒÂ룺
jspÒ³ÃæµÄ±àÂ뷽ʽÓÐÁ½¸öµØ·½ÐèÒªÉèÖãº
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
ÆäÖУºpageEncoding Ö¸µÄÊÇjspÎļþ±¾É ......