¡¾×ª¡¿JSP/ServletÖеıàÂë
Ê×ÏÈ£¬ËµËµJSP/ServletÖеļ¸¸ö±àÂëµÄ×÷Óá£
ÔÚJSP/ServletÖÐÖ÷ÒªÓÐÒÔϼ¸¸öµØ·½¿ÉÒÔÉèÖñà Â룬pageEncoding="UTF-8"¡¢contentType="text/html;charset=UTF-8"¡¢request.setCharacterEncoding("UTF-8")ºÍ response.setCharacterEncoding("UTF-8")£¬ÆäÖÐÇ°Á½¸öÖ»ÄÜÓÃÓÚJSPÖУ¬¶øºóÁ½¸ö¿ÉÒÔÓÃÓÚJSPºÍServlet ÖС£
1¡¢pageEncoding="UTF-8"µÄ×÷ÓÃÊÇÉèÖÃJSP±àÒë³ÉServletʱʹÓõıàÂë¡£
ÖÚËùÖÜÖª£¬JSPÔÚ·þÎñ Æ÷ÉÏÊÇÒªÏȱ»±àÒë³ÉServletµÄ¡£pageEncoding="UTF-8"µÄ×÷ÓþÍÊǸæËßJSP±àÒëÆ÷ÔÚ½«JSPÎļþ±àÒë³ÉServletʱʹÓÃµÄ ±àÂ롣ͨ³££¬ÔÚJSPÄÚ²¿¶¨ÒåµÄ×Ö·û´®£¨Ö±½ÓÔÚJSPÖж¨Ò壬¶ø²»ÊÇ´Óä¯ÀÀÆ÷Ìá½»µÄÊý¾Ý£©³öÏÖÂÒÂëʱ£¬ºÜ¶à¶¼ÊÇÓÉÓڸòÎÊýÉèÖôíÎóÒýÆðµÄ¡£ÀýÈ磬ÄãµÄ JSPÎļþÊÇÒÔGBKΪ±àÂë±£´æµÄ£¬¶øÔÚJSPÖÐÈ´Ö¸¶¨pageEncoding="UTF-8"£¬¾Í»áÒýÆðJSPÄÚ²¿¶¨ÒåµÄ×Ö·û´®ÎªÂÒÂë¡£
ÁíÍ⣬¸Ã²ÎÊý»¹ÓÐÒ»¸ö¹¦ÄÜ£¬¾ÍÊÇÔÚJSPÖв»Ö¸¶¨contentType²ÎÊý£¬Ò²²»Ê¹ÓÃresponse.setCharacterEncoding·½·¨Ê±£¬Ö¸¶¨¶Ô·þÎñÆ÷ÏìÓ¦½øÐÐÖØбàÂëµÄ±àÂë¡£
2¡¢contentType="text/html;charset=UTF-8"µÄ×÷ÓÃÊÇÖ¸¶¨¶Ô·þÎñÆ÷ÏìÓ¦½øÐÐÖØбàÂëµÄ±àÂë¡£
ÔÚ²»Ê¹ÓÃresponse.setCharacterEncoding·½·¨Ê±£¬ÓøòÎÊýÖ¸¶¨¶Ô·þÎñÆ÷ÏìÓ¦½øÐÐÖØбàÂëµÄ±àÂë¡£
3¡¢request.setCharacterEncoding("UTF-8")µÄ×÷ÓÃÊÇÉèÖöԿͻ§¶ËÇëÇó½øÐÐÖØбàÂëµÄ±àÂë¡£
¸Ã·½·¨ÓÃÀ´Ö¸¶¨¶Ôä¯ÀÀÆ÷·¢ËÍÀ´µÄÊý¾Ý½øÐÐÖØбàÂ루»òÕß³ÆΪ½âÂ룩ʱ£¬Ê¹ÓõıàÂë¡£
4¡¢response.setCharacterEncoding("UTF-8")µÄ×÷ÓÃÊÇÖ¸¶¨¶Ô·þÎñÆ÷ÏìÓ¦½øÐÐÖØбàÂëµÄ±àÂë¡£
·þÎñÆ÷ÔÚ½«Êý¾Ý·¢Ë͵½ä¯ÀÀÆ÷Ç°£¬¶ÔÊý¾Ý½øÐÐÖØбàÂëʱ£¬Ê¹ÓõľÍÊǸñàÂë¡£
Æä´Î£¬ÒªËµÒ»Ëµä¯ÀÀÆ÷ÊÇÔõôÑù¶Ô½ÓÊպͷ¢Ë͵ÄÊý¾Ý½øÐбàÂëµÄ
response.setCharacterEncoding("UTF- 8")µÄ×÷ÓÃÊÇÖ¸¶¨¶Ô·þÎñÆ÷ÏìÓ¦½øÐÐÖØбàÂëµÄ±àÂ롣ͬʱ£¬ä¯ÀÀÆ÷Ò²ÊǸù¾ÝÕâ¸ö²ÎÊýÀ´¶ÔÆä½ÓÊÕµ½µÄÊý¾Ý½øÐÐÖØбàÂ루»òÕß³ÆΪ½âÂ룩¡£ËùÒÔÔÚÎÞÂÛÄãÔÚ JSPÖÐÉèÖÃresponse.setCharacterEncoding("UTF-8")»òÕß response.setCharacterEncoding("GBK")£¬
Ïà¹ØÎĵµ£º
struts2µÄʵÏÖ·½·¨
LangSelector.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript">
<!--
function langSelecter_onChanged() {
& ......
package com.pan.encod;
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;
public class EncodFilter impl ......
1. jsp:forward±êÇ©Ö»ÄÜʵÏÖ±¾ÍøÕ¾ÄÚµÄÌøת£¬¼´ä¯ÀÀÆ÷ÉÏÏÔʾµÄÒÀÈ»ÊÇ֮ǰ¿Í»§¶ËµÄÒ³ÃæÎļþÃû£¬¶ø²»ÊÇÌøתºóµÄÒ³ÃæÎļþÃû
¶øresponse.sendRedirect()¿ÉÒÔÌøתµ½ÈκÎÒ»¸öµØÖ·µÄÒ³Ã棬ÇÒä¯ÀÀÆ÷ÉÏÏÔʾµÄÊÇÌøתºóµÄÒ³ÃæÎļþÃû
2. µ±ÔÚ֮ǰµÄ¿Í»§¶ËÒ³ÃæÎļþÖÐʹÓÃresponse.setAttribute()´æ´¢ÁËij¸öÖµºó£¬Ê¹ ......
request¶ÔÏóÊÇjavax.servlet.HttpServletRequestÀàµÄÒ»¸ö×ÓÀà¶ÔÏ󣬵±¿Í»§¶ËÇëÇóÒ»¸öJspÒ³ÃæÊÇ£¬JspÈÝÆ÷»á½«¿Í»§¶ËµÄÇëÇóÐÅÏ¢°ü×°ÔÚÕâ¸ö¶ÔÏóÖУ»¸Ã¶ÔÏóÖеij£Ó÷½·¨ÈçÏ£º
getParameter(String name)£ºÒÔ×Ö·û´®µÄÐÎʽ·µ»Ø¿Í»§¶Ë´«À´µÄijһ¸öÇëÇó²ÎÊýµÄÖµ£¬²ÎÊýÃûÓÉnameÖ¸¶¨
getParameterNames() ......
ºìÆì×ÀÃæLinux4.1Ï°²×°ºÍÅäÖÃJSP·þÎñÆ÷È«¹¥ÂÔ
ÕâÁ½ÌìÊÔ×ÅÔÚºìÆìLinux×ÀÃæ°æ4.1Ï°²×°WEB·þÎñÆ÷£¬Òѳɹ¦°²×°£¬ÏÖ°ÑÎҵݲװ¹ý³Ì½éÉÜÈçÏ£¬ÒÔ¸øÓÐÏàËÆÐèÇóµÄÍøÓѲο¼¡£
ÎҵݲװҪÇóÊÇÖ§³ÖJSP¡¢html¡¢htmÍøÒ³£¬ËùÒÔÑ¡ÓÃJDK1.5.0+Tomcat5.5.4+mySQL4.1.7£¬ÕâЩ¶¼Êǵ±Ç°¸÷Èí¼þ×îеÄÎȶ¨°æ±¾£¨·Ç²âÊ԰棩¡£ÔÝûÓа²×°Apacheº ......