Make JSP servers that do not support Chinese URL support Chinese URL (such as Tomcat)
Package filter; import java.io. *; import javax.servlet. *; import javax.servlet.http. *; import java.net. *;
Public Class Characterencoding Implements Filter {Protected FilterConfig FilterConfig = NULL; Private String Encoding = NULL; Public Void Destroy () {FilterConfig = NULL; Encoding = NULL;
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, "UTF-8") ;/ When IE INTNET Options -> Advanced Options -> The URL is always selected in UTF-8. INT k = url.indexof ("?"); string file = (k == - 1? URL: url.substring (0, k)); file f = new file (filter f = new file (fileconfig.getServletContext (). getRealPath (file) ); If (F.Exists () == false) {// URL is transmitted in URL = New String (S.GetBytes ("ISO-8859-1"), Encoding; URL = Urldecoder .decode (URL, Encoding); FilterConfig.getServletContext (). getRequestDispatcher (URL) .forward (req, response);}
Public void init (filterconfig filterconfig) throws servletexception {this.filterconfig = filterconfig; encoding = filterconfig.getinitParameter ("encoding");}}
The above is used for JDK1.4. If you use JDK1.3, add the following method in the above code and change urlencoder.decode (...) to decode (...):
public static String decode (String s, String enc) throws UnsupportedEncodingException {boolean needToChange = false; StringBuffer sb = new StringBuffer (); int numChars = s.length (); int i = 0; if (enc.length () == 0) {throw new unsupportedEncodingexception ("Urldecoder: Empty String Enc Parameter);} While (i