亂碼問題---HttpServletRequest物件增強
針對網站出現亂碼問題,其原因是request和response物件有關,我們知道對於post提交方法解決,可以通過呼叫request物件的setCharacterEncoding方法來設定其編碼方式,而對於response物件,也可以通過呼叫response的setCharacterEncoding方法設定編碼方式,及通過呼叫response的setContentType方法來指定text/html;charset的編碼方式。即:
request.setCharacterEncoding(charset); response.setCharacterEncoding(charset); response.setContentType("text/html;charset="+charset);
這樣可以解決關於request和response的亂碼問題,但是隻對post提交的方法有效,對get提交的方法無效。考慮到此問題,可以對request物件和response物件的方法進行增強,來解決request引起的的亂碼問題。下面的MyRequest是對HttpServletRequest的類的包裝類,而HttpServletRequestWrapper是預設實現了HttpServletRequest的包裝類,其中爭強的方法僅僅是呼叫了HttpServletRequest物件方法的方法。在實現對HttpServletRequest物件的包裝類時,可以繼承此類,這樣可以不需要對需要進行增強的方法進行重寫。僅僅重寫需要增強的方法即可。
針對get或者post提交的方法,其攜帶的資訊將以引數的形式帶到伺服器端,這個時候在伺服器端獲取時,可能要獲取到正確的引數值時,需要採用於提交時對這些值一致的編碼方式進行解碼。get方法與post不太一樣,不管對request指定什麼編碼方式,伺服器端獲取到的資料始終是以ISO8859-1解碼;因此上述方式通過設定request的字元編碼方式,只能對post提交的有效,對get方法無效,因此下面的MyRequest是對request進行增強,由於伺服器都是通過request.getParamter(String name)來獲取客戶端提交過來的資料,因此對其進行增強即可。增強後的MyRequest,將能解決由post和get提交方法產生的亂碼問題。
MyRequest為增強後的request類
public class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
private String charset;
//構造一個建構函式,申請此類時,將需要增強的request物件傳入,同時指定解碼方式
public MyRequest(HttpServletRequest request, String charset) {
super(request);
// TODO Auto-generated constructor stub
this.request = request;
this.charset = charset;
}
/* (non-Javadoc)
* @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
*/
@Override
public String getParameter(String name) {
// TODO Auto-generated method stub
String value = this.request.getParameter(name);
if(value ==null){
return null;
}
if(!request.getMethod().equalsIgnoreCase("get")){ //對get方法提交過來的資料進行特殊處理
return value;
}
try {
value= new String(value.getBytes("ISO8859-1"), this.charset);
System.out.println(value);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return value;
}
}
過濾器配置
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>cn.itcast.filters.EncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
過濾器
public class EncodingFilter implements Filter{
private FilterConfig config;
private String defaultCharset = "UTF-8";
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String charset = null;
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
charset = config.getInitParameter("charset");
System.out.println(charset);
if(charset == null || charset.equals("")){
charset = defaultCharset;
}
MyRequest myReq = new MyRequest(request, charset); //申請一個增強後的request物件
myReq.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset);
chain.doFilter(myReq, response); //放行後,servlet獲取的到request物件將是MyRequest物件,呼叫其getParamter是被增強後的
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.config = config;
}
}
測試jsp頁面
表單資料一般都是以post方法提交,而對於超連結,都是以Get方法提交,get方法提交,對中文需要先進行URL編碼後,在提交。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'charsetTest.jsp' starting page</title>
</head>
<body>
<c:url value="/servlet/CharsetTestServlet" scope="page" var="CharsetTestServlet">
<c:param name="username" value="中國"></c:param>
</c:url>
<a href="${CharsetTestServlet }">點選</a>
<form action="${pageContext.request.contextPath }/servlet/CharsetTestServlet" method="post">
使用者名稱<input type="text" name="username" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
測試servlet
public class CharsetTestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("username"));
}
}