中文编码采用GBK或者gb2312,前者支持的字符集合要大。
具体是:
(1)Jsp页面中设定:<%@ page contentType="text/html; charset=GBK" %>
(2)Servlet中,在response.getWriter()调用之前,执行response.setContentType(”text/html; charset=GBK")
(3)如果在Servlet中没有设定,自行编写toGBK函数,在取得参数时候进行转换,代码如下:
/**
* 做编码的转换,转换成GBK。
* @param str 需要做转换的字符串
* @return String 返回转换后的字符串
*/
public static String toGBK(String str) {
String temp = str;
try {
if (temp == null) {
temp = "";
}
if (temp.equals("null")) {
temp = "";
}
byte[] b = temp.getBytes("ISO8859-1"); //按字节将串拆分
str = new String(b, "GBK"); //按GBK编码方式将字节组合
} catch (UnsupportedEncodingException uee) {
}
return str;
}
(4)编写一个Servlet Filter,在每次响应之前进行编码的转换。这样可以避免在每个Servlet中进行上述的(2)(3)操作。Filter示例如下:
package kellerdu.util;
mport javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import net.sf.hibernate.*;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to true, any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* users session.</p>
*/public class SetEncodingServlet extends HttpServlet implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {this.encoding = null;
this.filterConfig = null;}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
/**
* 用户认证
*/
try {
HttpServletRequest req = (HttpServletRequest) request;
TblSysUser user = (TblSysUser) req.getSession().getAttribute(
LoginOper.USER);
if (user == null && req.getServletPath().indexOf("login") == -1) {//没有登录
req.getRequestDispatcher("/login.jsp").forward(request,response);
//chain.doFilter(request, response);
} else {
chain.doFilter(request, response);
//每次响应之后关闭Heibernate Session
HibernateUtil.closeSession();
}
} catch (ServletException ex) {
} catch (IOException ex) {
} catch (HibernateException ex) {
}
}/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
//System.out.println("SetEncodingServlet inits");
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
try {
com.yingrui.voip.HibernateUtil.closeSession();
} catch (HibernateException ex) {
System.out.println("Hibernate Init Exception!");
}}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);}
}
相应的web.xml配置如下(这里采用Struts):
<filter>
<filter-name>setencodingservlet</filter-name>
<filter-class>kellerdu.util.SetEncodingServlet</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setencodingservlet</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>setencodingservlet</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
相关推荐
Murachs Java Servlets and JSP(3rd) 无水印pdf
Murachs Java Servlets and JSP(3rd) 英文无水印pdf 第3版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
jsp与servlet数据交互出现null或???解决方案
jsp向servlet发送数据出现null: 检擦name值是否设置称中文,name值设置成中文servlet无法识别,会导致null的问题。 jsp接收servlet的数据出现null: 检查数据库是否连接成功,建议编写完beans以及DAO后,另外编写test.java测试数据库的增删查改是否正常。 jsp与serlvet交互数据出现???: 1.jsp中使用form则在form标签中添加accept-charset=“UTF-8” 。 2.jsp文件中的jsp代码如果访问request中的参
VMware虚拟机调用NVIDIA显卡全攻略:从3D加速到直通与vGPU
显卡虚拟化技术是虚拟化领域的关键组成部分,它通过在虚拟机中模拟或直接分配物理GPU资源,实现了图形渲染与通用计算的加速。其核心原理在于通过Hypervisor层对GPU指令进行拦截、转换或直接映射,从而平衡资源隔离与性能损耗。这项技术的核心价值在于提升资源利用率、实现计算环境隔离,并支持AI训练、3D渲染、科学计算等高性能负载的灵活部署。在应用场景上,轻量级图形界面加速、CUDA学习验证适合使用虚拟3D加速;而追求极致性能的机器学习训练、专业渲染则需依赖PCIe直通技术;对于企业级GPU资源池化与多租户共享
mysql,jsp,servlet中的中文编码问题解决
<br /><br />这几天因实验室发展需要~潜心研究jsp+servlet+mysql+tomcat网页开发,当然啦,重在功能的实现,特别是中文的正确处理是这次的一个重点任务,任务看似简单,但路途却充<br />满坎坷,本文将列举,一二,一是作为辛苦工作的真实回忆,二是作为技术贴供后来人(当然也包括自己啦)参考。<br />1. mysql中文编码问题:<br />今天下午终于在linux下又遇上了mysql数据库连接中的经典问题了,中文编码乱码问题,说它“又”,因为前几天在windows下用mysql
在Redroid容器中构建Frida RPC服务:自动化生成淘宝X-Sign签名参数
本文详细介绍了在Redroid容器中构建Frida RPC服务,实现自动化生成淘宝X-Sign等签名参数的完整方案。通过逆向分析定位关键方法,定制Redroid系统使Frida-Server常驻运行,并编写Frida脚本将签名方法暴露为RPC接口,最终封装为可远程调用的微服务,解决了签名生成的可移植性与工程化难题。
解决 TensorFlow 和 Keras 的 Python 版本兼容问题:详细技术指南
本文提供了TensorFlow和Keras与Python版本不兼容问题的完整解决方案。通过版本对照表指出不同TensorFlow版本对应的Python支持范围,强调2.13.x仅支持Python 3.9-3.11,且不兼容Python 3.12+。给出最佳实践建议:使用虚拟环境管理项目依赖,推荐venv或conda创建独立环境;指导正确安装TensorFlow和Keras版本,建议优先使用tf.keras避免冲突;针对常见报错如DLL加载失败、模块导入错误等提供具体解决方法。最后给出一个在兼容环境中运行的M
Servlet,JSP 中的中文乱码问题以及解决方案
问题描述: 在Servlet,JSP 传递数据中,英文无影响,而中文有时候就会出现乱码。 解决方案: 相同的编码: 同一个项目中的每个文件应当设置和保存相同的编码方式,如: html中 <meta charset="utf-8"> 在jsp 或者Servlet处理程序中添加: void setCharacterEncoding(java.lang.String env) 设置请...
STM32F4XX软件模拟I2C实现MPU6050姿态数据采集
本文详细介绍了在STM32F4XX平台上通过软件模拟I2C驱动MPU6050传感器实现姿态数据采集的完整方案。内容涵盖软件I2C的GPIO配置、核心通信函数实现、MPU6050寄存器配置详解以及数据读取处理流程,为硬件资源受限的嵌入式应用提供了灵活的解决方案,并分享了调试技巧与常见问题处理方法。
jsp servlet中文乱码的处理方法终极版
看完此博文,保证解决tomcat环境下jsp servlet中文乱码,不用再谷歌、百度了。还没解决的话,给我博客留言。 1.术语、约定 文中的乱码均指中文乱码,其他语言乱码不考虑 文中使用的环境是tomcat,其他环境不考虑 jsp、jsp文件指的是jsp源文件 j
单细胞测序流程(十)KEGG通路富集分析与可视化圈图实战解析
本文详细解析了单细胞测序数据分析中KEGG通路富集分析与可视化圈图的完整实战流程。从差异表达基因列表出发,通过R语言进行KEGG富集分析,并利用Perl脚本进行基因ID转换,最终使用GOplot包绘制高级可视化圈图,帮助研究者将基因列表转化为有意义的生物学故事。
解决Todesk连接卡在100%问题:remote display server is not supported (e.g. Wayland)
摘要:解决Ubuntu 22.04远程桌面连接问题(AnyDesk报错"remote display server not supported"及向日葵/Todesk无法显示)。方法:1)安装lightdm显示管理器;2)启用lightdm并禁用gdm;3)编辑/etc/gdm3/custom.conf文件,添加WaylandEnable=false;4)重启系统。注:后续可通过命令切换回gdm显示管理器。该方法适用于解决Wayland显示协议导致的远程桌面连接兼容性问题。
论servlet与jsp的关系
servlet 是一个特定格式的java文件。 servlet 是java版的页面 jsp 是页面版的java jsp就是servlet jsp主要负责界面效果展示 servlet主要负责后台业务逻辑 get/post 提交 get提交又叫作浏览器提交方式 速度快大小有限制2K 不安全 普通业务处理 post 提交又叫作服务器提交方式 大小没有限制 安全 一般用于文件上传和登录
【第1章>第8节】FNN模糊神经网络的理论学习与MATLAB仿真
模糊神经网络(FNN,Fuzzy Neural Network)是将模糊逻辑与神经网络相结合的一种智能算法,它融合了模糊逻辑善于处理不确定性和神经网络的自学习、自适应能力。
[JSP] 利用Filter解决Servlet输出中文乱码的问题
Servlet的过滤器, 用来解决中文输出乱码的问题. 1. Filter过滤器的代码如下: package filter;import java.io.IOException;import java.util.Collection;import java.util.Iterator;import java.util.Map;import java.util.Set;import...
链接文件及功能安全:S32K3如何将FLASH驱动放到RAM里面、RAM如何实现软件复位数据不丢失操作。
在通过RTD的SDK也好MCAL也好,始终存在一个问题,生成的代码除了看门狗模块,默认都是放在flash里面,按照正常逻辑来看没啥大的问题,思考一个问题:Flash放在0x100的位置,但是此时我们要操作0x100怎么办?那肯定不允许啊,但是将flash的驱动放在RAM里面就可以避免这个问题的产生。Flash的特性:PF0能操作PF1,当然PF1也能操作PF0的,以此类推,只要不操作自身,想怎么玩都可以。这也是一个物理特性吧。
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
中文乱码问题及其解决方法
中文乱码问题及其解决方法 1.解决HTML页面中的中文问题: 为了使HTML页面很好地支持中文,就必须在每个HTML页面的头部增加如下代码: ... ... 2.解决JSP页面中的中文问题 为了使JSP页面很好地支持中文,就必须在每个JSP页面的头部增
LinePhone3.2.7(android端最新版本) sip客户端
SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/
jsp传参servlet中文乱码问题
jsp传参servlet中文乱码问题,在jsp页面输入查询条件,英文和数字查询都能正常查询出结果,但是查询中文却查不出来(数据库中数据确认没问题),jsp页面和控制台也都能正常显示中文,但是把查询条件放到sql语句中就编程了问号,项目名称和jsp页面都是用的utf-8编码,百度了好久,以下方法也都试过了还是不行, 1,设置jsp页面的编码为utf-8 <%@ page langua...
TMS_WebGMaps_1.9.8.0_Full_Source
TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder
NTscan扫描工具
ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。
JSP和servlet中出现中文乱码的快速解决方法
jsp和servlet中出现中文乱码的情况很多,而且针对每种情况的解决方案都不太相同,现在我先简单的介绍一下出现乱码的各种情况以及相应的解决方案. 1.页面中的中文乱码. 例如: PrintWriter out = response.getWriter(); out.println("你好"); 在servlet中如果不加任何处理,这样做会出现乱码,解决方法就是在定义out对象之前插入这
20.JSP、Servlet
C/S (Client - Server 客户端-服务器端)典型应用:QQ软件 ,飞秋,有道笔记。B/S (Broswer -Server 浏览器端- 服务器端)典型应用: 腾讯官方(www.qq.com) 163新闻网站(俗称:网站)JavaWeb的程序就是b/s软件结构!!!常见的市面上 应用服务软件服务器作用:把服务器端的资源共享给外部访问。
jsp ,servlet 中文乱码问题
有时候jsp 网页中出现中文乱码现象,servlet 接受的数据如果是中文时会出现乱码。解决中文乱码问题有以下方法; 1.在页面设置 pageEncoding="gbk" 2.在servlet设置 request.setCharacterencoding("gbk"); response.rsetcharacterencoding("gbk"); 3.在Tomcat 的conf
图片名字是中文的解决方案
当我写到本章第七节(jsp一些其它相关话题)的时候,我有一个话题就是解决jsp下面的所有的中文的问题。但当图片名字是中文的时候,怎么都显示不出来图片。上网查了很长时间,也都没有理想的解决方案。有的说改变服务器的设置,有的说把图片的中文名字改成数字名字,之后把相关的信息存在数据库当中。有一个人建议用html的img标签指向Servlet。这个技术我早就知道,但是用在这里,这个想法还真是富有启发性。不...
JavaWeb | Servlet & JSP
Servlet
a b c JSP/Servlet中的汉字编码问题
1. 问题的起源 每个国家(或区域)都规定了计算机信息交换用的字符编码集,如美国的扩展 ASCII码, 中国的 GB2312-80,日本的 JIS 等,作为该国家/区域内信息处理的基础,有着统一编码的重要作用。字符编码集按长度分为 SBCS(单字节字符集),DBCS(双字节字符集)两大类。早期的软件(尤其是操作系统),为了解决本地字符信息的计算机处理,出现了各种本地
3095




被折叠的 条评论
为什么被折叠?



