Servlet的中文亂碼常用解決方法
在servlet頁面就出錯了,打印出就是亂碼,why?
public class toDetail extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
int id=Integer.parseInt(request.getParameter("ID"));
goodsBeanAction gba=new goodsBeanAction();
goodsBean gb=gba.getGoodsBean(id);
request.setAttribute("goodsInfo", gb);
request.getRequestDispatcher("showDetail.jsp教程").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
最佳答案
你的ID屬性是不是可以輸入中文的?如果是,則需要在接收參數之前設置字符編碼,request.setCharacterEncoding("gbk");,還有一個response.setContentType("text/html;charset=gbk");這樣就行了
具體如下:
1.JSP裡CHARSET要為GB2312
contentType="text/html;charset=GB2312"
2.SERVERLET類裡要有
request.setCharacterEncoding(gb2312);
3.字符集的重新格式化
java.net.URLEncoder java.net.URLDecoder 對應的encode 與decode進行編碼解碼。
在傳輸數據時候浏覽器會自動對要表單傳輸的數據進行url編碼,所使用的編碼方式取決於當前網頁顯示時候使用的編碼方式。
對於http請求消息的url地址後的參數,getparameter等方法進行自動url解碼時采用的編碼方式取決servlet引擎;tomcat默認用ISO8859-1進行解碼。
重新格式化語句如下:
String str1 = new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");
4.TOMCAT的CONF文件夾裡的SERVER.XML中大約92行左右.(如果你沒改過)
找到connector區塊,加入如下一行:
URIEncoding="GBK" 或 URIEncoding="GB2312" 或 URIEncoding="UTF-8"
完整的應如下:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups教程="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GB2312"
/>
5.Tomcat的conf文件夾下的web.xml文件的<servlet></servlet>標簽之間添加如下代碼
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
6.使用Servlet規范中的過慮器指定編碼
<1>.新創建一個servlet時,Interfaces接口:繼承javax.servlet.Filter
options中只需選中:create Inherited Methods; create Constructors;
建好後在其doFilter()方法中:
arg0.setCharacterEncoding("gb2312"); //實現請求亂碼的處理
arg1.setCharacterEncoding("gb2312"); //實現響應亂碼的處理
arg2.doFilter(arg0,arg1); //繼續執行其他過濾器 或 jsp、servlet
<2>.再在web.xml中把過濾器servlet中的相關內容的<servlet></servlet>改為<filter></filter>
並修改<filter-mapping></filter-mapping>中的<url-patterm>/*</url-patterm> ; “/*”表示執行任何文件
簡介過濾器:
A.每個過濾器都會在 web.xml中有單獨的配置:
<filter>
<filter-name>過濾器的別名</filtr-name>
<filter-class>過濾器的物理地址,帶有完整的包路徑的</filter-class>
</filter>
<filter-mapping>
<filter-name>過濾器的別名</filter-name>
<filter-patterm>過濾器訪問的路徑</filter-patterm>
</filter-mapping>
B.當由於某種原因想要刪除servlet類時,刪除後在web.xml中還會保留刪除的servlet類的記錄,
所以必須手動在web.xml中刪除一下已刪除的servlet類的信息
過濾器的在web.xml中的典型配置和主要代碼如下:
web.xml:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>net.vschool.web.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
CharacterEncodingFilter.java:
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 CharacterEncodingFilter implements Filter
{
protected String encoding = null;
public void init(FilterConfig filterConfig) throws ServletException
{
this.encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
}
}7.解決數據庫教程的亂碼
在jdbc.url中明確字符集 (不適用於sqlserver)
在連接數據庫時:con=DriverManager.getConnection(url,"sa","");
url="jdbc:協議:子協議://IP:端口; 庫 ? useUnicode=true & characterEncoding=gb2312";
過濾器不就得了
public class Filter1 implements Filter {
private FilterConfig _filterConfig = null;public void init(FilterConfig filterConfig) throws ServletException {
_filterConfig = filterConfig;
}public void destroy() {
_filterConfig = null;
}public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {chain.doFilter(request, response);
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
}
}
然後在web.xml中<?xml version = '1.0' encoding = 'GBK'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<description>Empty web.xml file for Web Application</description>
<filter>
<filter-name>Filter1</filter-name>
<filter-class>project1.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>*.*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>35</session-timeout>
</session-config>
<mime-mapping>
<extension>html</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
</web-app>