首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁Tomcat:URL中文编码设置”︁的源代码
←
Tomcat:URL中文编码设置
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
tomcat中,如果不设置URL编码,URL中带有中文字符时会导致乱码。解决方法是对URL进行编码,并在tomcat中进行设置。 server.xml <pre> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /> </pre> =测试TOMCAT编码= 来写个小程序测试下: <source lang="java"> public class HelloServlet extends HttpServlet{ @Override public void doGet(HttpServletRequest request, HttpServletResponse response){ System.out.println("=>GET"); System.out.println(request.getRequestURI()); this.printParam(request); } void printParam(HttpServletRequest request){ Map<String, String[]> params = request.getParameterMap(); for(String k:params.keySet()){ System.out.print("\t?" + k + "="); for(String v:params.get(k)) System.out.println("(" + v + ")"); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response){ System.out.println("=>POST"); this.printParam(request); } } </source> web.xml中配置: <source lang="java"> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>com.riguz.tc.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </source> 然后是一个小的测试程序: <source lang="java"> public static void main(String[] args) { final String url = "http://localhost:8080/tc/hello"; HttpKit.get(url + "?id=COM_RIGUZ_测试_0001"); HttpKit.post(url + "?id=COM_RIGUZ_测试_0001", "name=中华人民共和国&id=COM_RIGUZ_测试_0001"); } public static String post(String url, Map<String, String> queryParas, String data, Map<String, String> headers) { HttpURLConnection conn = null; try { conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers); conn.connect(); OutputStream out = conn.getOutputStream(); out.write(data.getBytes(CHARSET)); out.flush(); out.close(); return readResponseString(conn); } catch (Exception e) { throw new RuntimeException(e); } finally { if (conn != null) { conn.disconnect(); } } } </source> =GET乱码= 如果不加任何配置,一个tomcat运行起来后,输出是这样子的: <pre> =>GET /tc/hello ?id=(COM_RIGUZ_???è??_0001) =>POST ?id=(COM_RIGUZ_???è??_0001) (COM_RIGUZ_???è??_0001) ?name=(??????????°???±??????) </pre> 我们在tomcat的server.xml加上URL编码,再看看输出: <source lang="xml"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> </source> 可以看出URL部分的参数已经正常了,不过POST部分还是不对。 <pre> =>GET /tc/hello ?id=(COM_RIGUZ_测试_0001) =>POST ?id=(COM_RIGUZ_测试_0001) (COM_RIGUZ_???è??_0001) ?name=(??????????°???±??????) </pre> <source lang="java"> </source> <source lang="java"> </source> =POST乱码= 我们试试在post方法里面加点东西: <source lang="java"> conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); </source> 然后就正常了! <pre> =>GET /tc/hello ?id=(COM_RIGUZ_测试_0001) =>POST ?id=(COM_RIGUZ_测试_0001) (COM_RIGUZ_测试_0001) ?name=(中华人民共和国) </pre> [[Category:Linux/Unix]]
返回
Tomcat:URL中文编码设置
。