修改配置文件如下:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
com.softeem.crm.filter.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
----------------------------------------------------------------------------------------------------------------------------
filter 类如下:
/*
* Created on Sep 27, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* <p>Example 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 user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 1.5 $ $Date: 2005/03/21 18:08:09 $
*/
public class SetCharacterEncodingFilter 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);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
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"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// -------------------------------------------------- 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);
}
}
分享到:
相关推荐
- **初始化阶段**:当服务器启动时,会加载并实例化Servlet。 - **请求处理阶段**:每当客户端发出请求,服务器就会调用Servlet的`service()`方法进行处理。 - **销毁阶段**:当服务器关闭时,会调用`destroy()`...
这些源代码通常包含EJB、Servlet、JSP、Filter、Listener等不同类型的组件。在IDE中,源码会被编译成字节码,然后由应用服务器加载并执行。 【工具】标签可能涉及到与J2EE开发相关的各种工具,如Maven进行项目管理...
Servlet容器(如Tomcat)负责管理Servlet的生命周期,确保其正确加载和执行。 2. **JSP(JavaServer Pages)**:JSP是一种动态网页技术,允许开发者在HTML代码中嵌入Java代码,通过JSP标签库(Tag Libraries)和EL...
6. **Filter和Listener**:Servlet API还提供了Filter和Listener接口,用于在请求到达Servlet之前进行预处理(Filter),或者监听Servlet生命周期及Web应用中的各种事件(Listener)。 7. **MVC模式**:Servlet常与...
Servlet API定义了Servlet类和Filter类,用于控制请求和响应流程。 2. **JSP(JavaServer Pages)**:JSP是一种视图技术,用于创建动态HTML、XML或其他格式的Web内容。开发者可以在JSP页面中混合HTML和Java代码,...
过滤器(Filter)是J2EE中一种强大的工具,可以在请求到达Servlet之前或之后进行预处理和后处理。韩顺平老师会演示如何实现自定义过滤器,如字符编码转换、登录验证等场景。同时,监听器(Listener)则可以监听Web...
在Servlet 2.3及以上版本中,可以通过实现`javax.servlet.Filter`接口创建自定义过滤器,并在`Web.xml`中配置,确保在每次请求时调用过滤器以清除缓存头信息,防止敏感内容被保存在客户端。 此外,文中提到了J2EE...
这样做是为了让Tomcat服务器能够识别并加载Struts2的库文件。 然后,你需要创建一个新的Web工程。在你的编程环境中(如Eclipse、IntelliJ IDEA等),选择Tomcat对应的版本,新建一个Web项目。在项目的`WebContent`...
- **Annotations-based Deployment**:不再需要web.xml,可以在类上直接使用注解声明Servlet、Filter和Listener。 - **Asynchronous Support**:允许异步处理请求,提高并发性能。 - **Pluggable Servlet ...
**J2EE程序设计——Servlet基础与编程** Servlet是一种基于Java的服务器端应用程序,用于扩展Web服务器的功能,能够生成动态的Web页面。Servlet不同于Applet,后者运行在客户端且拥有图形用户界面,而Servlet则运行...
在J2EE应用开发中,优化资源加载速度和减少网络传输的数据量是非常重要的。其中,gzip压缩技术就是一种常见的优化手段,它能显著地减小HTML、JavaScript、CSS等静态资源的大小,从而提高页面加载速度,提升用户体验...
- 实现`javax.servlet.Filter`接口。 - 重写`init()`、`doFilter()`和`destroy()`方法。 ##### 11.3 创建设置请求编码格式过滤器 - 设置请求的字符编码。 ##### 11.4 创建登录验证的过滤器 - 验证用户是否已登录。...
在Java企业版(J2EE,现在称为Java EE)中,过滤器(Filter)是一个强大的工具,用于在请求被处理之前或之后对HTTP请求和响应进行拦截和处理。过滤器可以应用于整个应用程序或者特定的资源,例如Servlet、JSP页面等...
【标题】"j2EE Servlet" 是一个与Java企业版(Java Enterprise Edition,简称J2EE)中的Servlet技术相关的主题。Servlet是Java编程语言中用于动态处理Web请求的一种核心组件,它扩展了Web服务器的功能,使得服务器...
1. 加载与初始化:当Servlet容器首次接收到对Servlet的请求时,会加载Servlet类并实例化,然后调用init()方法进行初始化。 2. 服务:每次客户端请求到达,都会调用service()方法,该方法会根据请求类型分派到doGet()...
8. ****: 此元素用于定义Servlet,包括其类名和初始化参数,使得Servlet能够在应用启动时被加载和初始化。 9. ****: 通过这个元素,你可以映射Servlet到特定的URL,从而控制用户如何访问Servlet。 10. ****: 用于...
### J2EE课程总结 #### 数据库:Oracle **1. Oracle SQL基础知识** - **选择行**:通过`SELECT`语句结合`WHERE`子句来实现特定条件下的数据筛选。 - **限制选择行**:利用`LIMIT`或`ROWNUM`来限制返回结果的数量...
- **初始化**:当Servlet首次被加载时,容器会调用`init()`方法进行初始化。 - **服务处理**:客户端发送请求后,容器会调用`service()`方法来处理请求。在该方法内部,通常会根据请求类型(GET或POST等)调用相应的...