Tomcat与Web.xml配置
1、编码配置
<Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableLookups=”false” maxHttpHeaderSize=”8192″ maxSpareThreads=”75″ maxThreads=”150″ minSpareThreads=”25″ port=”80″ redirectPort=”8443″ URIEncoding=”GBK”useBodyEncodingForURI=”true” />
url使用GBK进行编码。
URIEncoding:用来设定通过 URI 传递的内容使用的编码,tomcat 将使用这里指定的编码对客户端传送的内容进行编码。我们通过 get 方法提交的参数实际上都是通过 uri 提交的,由这个参数管理,如果没有设定这个参数,则 tomcat 将使用默认的 iso8859-1 对客户端的内容进行编码。
useBodyEncodingForURI:使用与 Body 一样的编码来处理 URI, 这个设定是为了与 tomcat4保持兼容,原来在 tomcat4 和 tomcat5 中队参数的处理是不一样的,在 tomcat4 中 get 与 post 的编码是一样的,所以只要在过滤器中通过 request.setCharacterEncoding 设定一次就可以解决 get 与 post 的问题。然而,在 tomcat5 中,get 与 post 的处理是分开进行的,对 get 的处理通过前面的 URIEncoding进行处理,对 post 的内容依然通过 request.setCharacterEncoding 处理,为了保持兼容,就有了这个设定。
所以,设置URIEncoding解决get中的参数问题,配置过滤器解决post的参数问题;或者设置useBodyEncodingForURI为true,get、post都使用过滤器来解决参数问题。
2、网页编码可以设置请求头:在head中添加
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
3、修改浏览器配置
ie:“internet选项/高级”中“总以utf-8发送网址”的选项的配置;
firefox:地址栏中输入about:config,修改network.standard-url.escape-utf8 为False (缺省为True);
如果你想让浏览器直接url-encode成utf-8,修改network.standard-url.encode-utf8为true(缺省为false)。
4、使用Spring时可能还有如下配置
主要针对与数据库交互时产生的乱码问题,例如插入中文记录或读取中文数据。
<property name=”driverClassName”>
<value>com.mysql.jdbc.Driver</value>
</property>
<property name=”url”>
<value>jdbc:mysql://localhost:3306/newfang?useUnicode=true&characterEncoding=gbk&autoReconnect=true</value>
</property>
5、代码中解决乱码问题
主要用于解决个别乱码问题,例如网页显示时中文均显示正常除一两条语句外,那么针对这一两条语句的乱码问题可用该方法。
qString = new String(qString.getBytes(“ISO8859_1″), “GBK”);
或 qString = URLEncoder.encode(qString, “GBK”); //java url 编码方法
5、高级,web.xml配置,添加filter过滤器
用于处理全站乱码问题,其实主要也是用于action与jsp页面交互时使用。
<!– 用于解决中文乱码问题 –>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.qa.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SetEncodingFilter代码:
package com.qa.util;
import javax.servlet.*;
import java.io.*; public class SetEncodingFilter implements Filter
{
protected String encoding=null;//定义缺省字符编码方式
protected boolean ignore=true;//定义客户端指定的编码方式是否应被忽略
protected FilterConfig filterConfig=null;//定义过滤器配置对象,若为null,则说明过滤器未配置 public void destroy()//停止过滤器的工作
{
this.encoding=null;
this.filterConfig=null;
}
//设置字符编码
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
throws IOException,ServletException
{
if(ignore||(req.getCharacterEncoding()==null))
{
req.setCharacterEncoding(selectEncoding(req));
}
chain.doFilter(req,res);
}
//启动过滤器
public void init(FilterConfig filterConfig)throws ServletException
{
this.filterConfig=filterConfig;
this.encoding=filterConfig.getInitParameter(“encoding”);
String value=filterConfig.getInitParameter(“ignore”);
if(value==null) this.ignore=true;
else if(value.equalsIgnoreCase(“true”)
||value.equalsIgnoreCase(“yes”)) this.ignore=true;
else this.ignore=false;
}
//选择合适的字符编码方式
protected String selectEncoding(ServletRequest req)
{
return this.encoding;
}
//返回filterConfig对象
public FilterConfig getFilterConfig()
{
return filterConfig;
}
//设置filterConfig对象
public void setFilterConfig(FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
}
6、服务器apache上的乱码。
除了以上的情况外,还有apache的配置问题,注意的方面有以下几点:
1)conf/httpd.conf
把AddDefaultCharset ISO-8859-1 改成 AddDefaultCharset GBK
2)apache进行了rewrite
把需要rewrite的url中的中文参数进行两次编码(encode),因为apache在rewrite时会做一次url解码,这时jk进行请求转发时,就不会再是编码后的字符串了;
或者在接收请求时先用ISO-8859-1取字节流,再使用UFT-8来new String。(new String(str.getBytes(“ISO-8859-1″),”UFT-8″))
http://blog.sina.com.cn/s/blog_6310009d01014v9d.html
Tomcat与Web.xml配置的更多相关文章
- Servlet容器Tomcat中web.xml中url-pattern的配置详解[附带源码分析]
目录 前言 现象 源码分析 实战例子 总结 参考资料 前言 今天研究了一下tomcat上web.xml配置文件中url-pattern的问题. 这个问题其实毕业前就困扰着我,当时忙于找工作. 找到工作 ...
- tomcat web.xml配置
关于Tomcat 中 web.xml 文件的配置问题: 1.下面的配置是合法的 <servlet> <servlet-name>test</serv ...
- 在tomcat下部署两个或多个项目时 log4j和web.xml配置webAppRootKey 的问题(转)
在tomcat下部署两个或多个项目时 web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为"webapp.root",如下: <!-- 应用路径 ...
- Tomcat web.xml配置参数详解
Apache Tomcat Configuration Reference - The Context Containerhttps://tomcat.apache.org/tomcat-5.5-do ...
- 如何通过配置tomcat或是web.xml让ie直接下载文件
web.xml(tomcat\conf\web.xml)中配置了 <mime-mapping> <extension>txt</extension> < ...
- java web.xml配置详解
1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...
- Spring MVC的web.xml配置详解(转)
出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...
- web.xml 配置介绍
这个不是原创,有点早了,具体从哪里来的已经记不得了.但是东西是实实在在的. 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<c ...
- web.xml配置详解之listener与context-param
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context- ...
随机推荐
- VMware网络模式介绍
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式). Bridged 模式: 在桥接模式下,VMware虚拟机里的系统就像是 ...
- 机器人操作系统(ROS)教程22:ROS的3D可视化工具—rviz
rviz是ROS中的一个3D可视化工具,有了它就可以把你用代码建的机器人模型转化为可视的3D模型. 首先需要安装: rosdep install rviz 然后编译rviz: rosmake rviz ...
- debian的bt下载工具
sudo apt install qbittorrent 如果觉得下载慢,可以选择下载的文件,在trackers部分修改 https://github.com/ngosang/trackerslist
- 蓝桥杯 算法训练 ALGO-36 传纸条
算法训练 传纸条 时间限制:1.0s 内存限制:512.0MB 问题描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而 ...
- 解决Maven提示:Could not read settings.xml, assuming default values
本文转载自:http://blog.csdn.net/hqocshheqing/article/details/47702049 最近在学习Maven 时总是出现 Could not read se ...
- git 远程仓 和 本地仓 记录
一.远程仓添加信息后 ,本地环境修改信息后,上传 远程仓 coding=utf-8 本地仓 coding=utf-8 本地仓同步后 会提示存在冲突: (其中 HEAD 是当前非支的意思,可以理解为当前 ...
- 1041 Be Unique
题意:找到一串数字序列中首个出现的不重复的数字. 思路:用哈希,因为数值大小在[1,10^4],所以可以直接开数组.输入数据时记录每个数字出现过的次数.然后遍历原序列,遇到第一个次数为1的数字就是所求 ...
- Py修行路 内置模块补充 datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.datetime模块用于是date和time模块的合集,他内部重新封装了time模块,相比于time ...
- python's fourteenth day for me 内置函数
locals: 函数会以字典的类型返回当前位置的全部局部变量. globals: 函数会以字典的了类型返回全部的全局变量. a = def func(): b = print(locals()) ...
- DDD学习笔录——提炼问题域之与领域专家一起获得领域见解
业务和开发团队之间的协作是DDD必不可少的部分,并且它是处于开发阶段的产品获得成功的关键. 领域专家指的是那些从业务领域的政策和工作流程到棘手处和特性都具有深刻理解的人.能够为你的问题区域提供深刻见解 ...