tomcat Content-Language:en-US 导致乱码

本文介绍如何解决Linux服务器上Tomcat应用的响应头语言显示为英语的问题,通过修改服务器默认语言环境来确保页面的Content-Language正确显示为中文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天发现我们网站的页面Response Headers部分的语言显示为英语,Content-Language:en-US,而不是熟悉的Content-Language:zh-CN。即使在页面的meta信息里面写上Content-Language:zh-CN也不管用。

一番折腾以后,发现这个语言和linux服务器的默认语言有关系,用“locale”命令查看。

启动tomcat的那个用户使用的默认语言,就会被当做Response Headers部分的语言。

了解了这个就好办了,把运行tomcat用户的默认语言设置成中文的UTF-8:

修改这个文件:/etc/default/locale,添加:

lang="zh_cn.utf-8"

(如果/etc/default/locale文件不存在,可以在/etc/bashrc文件最后面,添加“export LANG=zh_CN.utf8”,保存,退出重登录即可。)

保存以后,用locale命令看看:

lang=zh_cn.utf-8
lc_ctype="zh_cn.utf-8"
lc_numeric="zh_cn.utf-8"
lc_time="zh_cn.utf-8"
lc_collate="zh_cn.utf-8"
lc_monetary="zh_cn.utf-8"
lc_messages="zh_cn.utf-8"
lc_paper="zh_cn.utf-8"
lc_name="zh_cn.utf-8"
lc_address="zh_cn.utf-8"
lc_telephone="zh_cn.utf-8"
lc_measurement="zh_cn.utf-8"
lc_identification="zh_cn.utf-8"
lc_all=


ok,然后重启tomcat就能生效了。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="zh-CN" class="bootstrap-admin-vertical-centered"> <head> <meta charset="UTF-8"> <title>图书馆管理系统</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="static/css/bootstrap.min.css"> <link rel="stylesheet" href="static/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="static/css/bootstrap-admin-theme.css"> <link rel="stylesheet" href="static/css/bootstrap-admin-theme.css"> <script src="static/js/bootstrap.min.js"></script> <script src="static/jQuery/jquery-3.1.1.min.js"></script> <script src="static/ajax-lib/ajaxutils.js"></script> <script src="static/js/login.js"></script> </head> <style type="text/css"> .alert{ margin: 0 auto 20px; text-align: center; } </style> <script src="static/js/jquery.min.js"></script> <script src="static/js/bootstrap.min.js"></script> <body class="bootstrap-admin-without-padding"> 池州学院 230312242 杨洪博 课设 <div style="background-image: url('/books/WebContent/2.png'); background-size: cover; background-repeat: no-repeat; background-position: center;"></div> <div class="col-lg-12"> <div class="alert alert-info"> <a class="close" data-dismiss="alert" href="#">×</a> 欢迎登录图书馆管理系统 </div> <form class="bootstrap-admin-login-form" method="post" action="/books/LoginServlet"> <% String state = (String)session.getAttribute("state"); session.removeAttribute("state"); if(state!=null){ %> <label class="control-label" for="username">密码错误</label> <%}%> <div class="form-group"> <label class="control-label" for="username">账 号</label> <input type="text" class="form-control" id="username" name="username" required="required" placeholder="学号"/> <label class="control-label" for="username" style="display:none;"></label> </div> <div class="form-group"> <label class="control-label" for="password">密 码</label> <input type="password" class="form-control" id="password" name="password" required="required" placeholder="密码"/> <label class="control-label" for="username" style="display:none;"></label> </div> <label class="control-label" for="password">没有账号请<a href="/books/register.jsp" style="color:blue;">注册</a></label> <br> <input type="submit" class="btn btn-lg btn-primary" value="登    录"/> </form> </div> </div> </div> <div class="modal fade" id="modal_info" tabindex="-1" role="dialog" aria-labelledby="addModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="infoModalLabel">提示</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-lg-12" id="div_info"></div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" id="btn_info_close" data-dismiss="modal">关闭</button> </div> </div> </div> </div> </body> </html> 图片不显示 是哪里出了问题
最新发布
06-22
### 解决 Tomcat 8.5.16 中文乱码问题 #### 修改 `server.xml` 配置文件 为了处理中文字符编码问题,在 `/usr/local/tomcat8/conf/server.xml` 文件中找到 `<Connector>` 节点并添加 URIEncoding 属性: ```xml <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/> ``` 此设置确保所有请求参数都按照 UTF-8 编码进行解析[^1]。 #### 设置环境变量 编辑启动脚本或者全局 profile 文件来定义 JAVA_OPTS 参数,加入 `-Dfile.encoding=UTF-8` 来强制 JVM 使用 UTF-8 字符集: 对于 Linux 系统可以在 `/etc/profile` 或者 `$CATALINA_HOME/bin/setenv.sh` 添加如下内容: ```bash export JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8" ``` 这一步骤可以保证整个应用程序运行期间始终采用统一的字符编码方式[^2]。 #### Web 应用程序内部调整 如果上述方法仍无法彻底解决问题,则需检查具体 web application 的 JSP 页面头部是否有正确的 charset 声明;另外还需确认表单提交时是否指定了合适的 accept-charset 属性。例如: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="" method="post" accept-charset="utf-8"> <!-- form elements --> </form> </body> </html> ``` 通过以上措施应该能够有效解决大部分场景下的中文显示异常情况[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值