2 qq 28658245 qq_28658245 于 2017.01.11 10:17 提问

tomcat7 get请求乱码,求大神解惑呀

在做一个项目时,get请求发送中文参数出现乱码,框架是ssh框架
已经设置了springMVC的过滤器,tomcat中server.xml也已经指定编码,jsp页面也指定了编码,但是发送get请求是还是乱码

web.xml

 <web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/applicationContext*.xml</param-value>
  </context-param>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>Spring OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>sessionFactoryBean</param-name>
      <param-value>sessionFactory</param-value>
    </init-param>
    <init-param>
      <param-name>flushMode</param-name>
      <param-value>ALWAYS</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Spring OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> 
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/SpringMVC*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

server.xml

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8"
               useBodyEncodingForURI="true"/>

jsp:

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

2个回答

bjjoy2009
bjjoy2009   2017.01.11 10:22

这个get需要添加编码,在发送请求的地方把中文参数staff改为encodeURI(encodeURI(staff)),
在后台处理的地方进行解码,URLDecoder.decode(staff,"UTF-8")

shao861210
shao861210   2017.01.11 10:50

这个get需要添加编码,在发送请求的地方把中文参数staff改为encodeURI(encodeURI(staff)),
在后台处理的地方进行解码,URLDecoder.decode(staff,"UTF-8");楼上正解

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!