使用Filter进行转码,首先在web.xml中对Filter进行部署
<filter>
<filter-name>deCode</filter-name>
<filter-class>cn.com.Demo.Filter.DeCode</filter-class>
<init-param>
<param-name>code</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>deCode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后新建一个Class实现Filter接口,其中需要注意的是,req.setCharacterEncoding()只支持post方法,如要get和post都支持可以采用第一种方法是从requset中将参数获取出来,然后手动转码(从iso-8859-1转为utf-8),因为request也是一个作用域对象,所以可以用来传递已经转码后的参数,使用setAttribute,有一点需要注意的是在获取参数时,原本的request.getParameter()就应该对应更改为request.getAttribute()。
public class DeCode implements Filter{
private String code = "UTF-8";
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("进入过滤器");
//req.setCharacterEncoding(code); 只支持post方法
Enumeration<String> paraNames = req.getParameterNames();
while(paraNames.hasMoreElements())
{
String paraName = paraNames.nextElement();
String value = req.getParameter(paraName);
byte[] bytes = value.getBytes("iso-8859-1");
value = new String(bytes,"utf-8");
req.setAttribute(paraName,value);
}
res.setContentType("text/html;charset="+code);
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig cfg) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化过滤器");
String encode = cfg.getInitParameter("code");
if(encode!=null)
{
System.out.println("当前编码为"+encode);
this.code = encode;
}
}
由于第一种方法需要将每个servlet中的getParameter改为getAttribute,所以推荐使用第二种方法Wrapper(包装器)。
代码如下:
public class DeCode implements Filter{
private String code = "UTF-8";
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("进入过滤器");
/* new包装器,此处需要注意,因为doFilter中是ServletRequest,
* 所以要将其转换为HttpServletRequest*/
MyRequestWrapper reqWrapper = new MyRequestWrapper((HttpServletRequest)req);
res.setContentType("text/html;charset="+code);
chain.doFilter(reqWrapper, res);//将request替换成包装器的reqWrapper
}
@Override
public void init(FilterConfig cfg) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化过滤器");
String encode = cfg.getInitParameter("code");
if(encode!=null)
{
System.out.println("当前编码为"+encode);
this.code = encode;
}
}
/*包装器内置类*/
private class MyRequestWrapper extends HttpServletRequestWrapper
{
public MyRequestWrapper(HttpServletRequest request) {
super(request);
// TODO Auto-generated constructor stub
}
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
value = this.myEnCoding(value);
return value;
}
@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
if(values != null)
{
for(String value:values)
{
value=this.myEnCoding(value);
}
}
return values;
}
private String myEnCoding(String value)
{
if(value != null)
{
try{
byte[] bytes = value.getBytes("iso-8859-1");
value = new String(bytes,code);
}catch (Exception e) {
// TODO: handle exception
}
}
return value;
}
}
}
通过过滤器对登陆权限进行控制
通过设置一个过滤器,请求中没有登陆信息时则返回到登陆界面。
代码如下:
public class LoginControlFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
//判断是否有登陆
HttpServletRequest request = (HttpServletRequest)req;
HttpSession session = request.getSession();
String userName = (String)session.getAttribute("LOGIN_USER");
if(userName != null&&!"".equals(userName))
{
//如有登陆则继续调用后续
arg2.doFilter(req, res);
}else
{
//如没登陆则跳转到LoginServlet
req.getRequestDispatcher("/loginServlet").forward(req, res);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
在Web.xml中进行如下配置
<filter>
<filter-name>LoginControlFilter</filter-name>
<filter-class>cn.com.Demo.Filter.LoginControlFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginControlFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>