过滤器

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

EncodingFilter.java的书写 :

package org.whc;

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;

public class EncodingFilter implements Filter {

protected String encoding = null;
protected FilterConfig config;

public void init(FilterConfig filterConfig) throws ServletException{//初始化操作
this.config = filterConfig;
//得到在web.xml中配置的编码
this.encoding = filterConfig.getInitParameter("Encoding");//读取配置文件中的编码方式
}
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,ServletException{
if(request.getCharacterEncoding() == null){//如果页面的没有编码方式,我们使用过滤器来加载
//得到指定的编码
String encode = getEncoding();
if(encode != null){
//设置request的编码
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
}
chain.doFilter(request,response);
}
protected String getEncoding(){
return encoding;
}
public void destroy(){
}
}

配置XML:

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>code.EncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>

<!-- EncodingFilter mapping -->
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

你可能感兴趣的:(C++,c,xml,Web,servlet)