Tomcat等JAVA应用服务器,默认情况下是不支持中文的URL访问路径的,我们可以通过自己添加滤镜的方法让其支持,具体代码如下:
2009年3月11日星期三
让JAVA应用服务器支持中文URL路径
package com.suntek.draw.common;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.net.*;
/**
* <p>Title: </p>
*
* <p>Description: 让Tomcat支持中文路径</p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company:健新科技 </p>
*
* @author wmzsoft
* @version 1.0
*/
public class DwfFilter implements Filter {
protected FilterConfig filterConfig = null;
private String encoding = null;
public void destroy() {
filterConfig = null;
encoding = null;
}
/**
* 让Tomcat支持中文路径。
* @param request ServletRequest
* @param response ServletResponse
* @param chain FilterChain
* @throws IOException
* @throws ServletException
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String s = req.getRequestURI();
String url = URLDecoder.decode(s, encoding); //当IE中Intertnet选项-高级选项-总是以UTF-8发送URL 被选中时。
String context = req.getContextPath();
if ((url !=null) && (context !=null)){
int idx = url.indexOf(context);
if (idx>=0){
url = url.substring(idx+context.length());
}
}
filterConfig.getServletContext().getRequestDispatcher(url).forward(req,response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
encoding = filterConfig.getInitParameter("encoding");
}
}
<filter>
<filter-name>DWFFilter</filter-name>
<filter-class>com.suntek.draw.common.DwfFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
订阅:
博文评论 (Atom)
0 comments:
发表评论