您的位置:首页 > 运维架构 > 网站架构

购物网站21:过滤器----购物车---用户登录---员工登录---登录验证---编码转换

2009-09-01 00:39 561 查看
public class BuyCartFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
BuyCart cart = WebUtil.getBuyCart(request);
if(cart==null || cart.getItems().isEmpty()){
request.setAttribute("message", "您的购物车里面没有商品,请购买商品后再执行此操作");
request.setAttribute("urladdress", "/");
request.getRequestDispatcher("/WEB-INF/page/share/message.jsp").forward(req, res);
return ;
}
chain.doFilter(req, res);
}

public void init(FilterConfig arg0) throws ServletException {
}

}


-----------------------------------------------------------------------------

public class BuyerLogonFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
Buyer buyer = WebUtil.getBuyer(request);
if(buyer==null){
HttpServletResponse response = (HttpServletResponse) res;
String url = WebUtil.getRequestURIWithParam(request);
String encodeurl = new String(Base64.encodeBase64(url.getBytes()));
response.sendRedirect("/user/logon.do?directUrl="+ encodeurl);
return;
}
chain.doFilter(req, res);
}

public void init(FilterConfig arg0) throws ServletException {
}

}

--------------------------------------------------------------------------

/**
* 校验员工是否登录
*
*/
public class EmployeeFilter implements Filter {

public void destroy() {}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
Employee employee = WebUtil.getEmployee(request);
if(employee==null){
HttpServletResponse response = (HttpServletResponse)res;
response.sendRedirect("/employee/logon.do");
return;
}
chain.doFilter(req, res);
}

public void init(FilterConfig arg0) throws ServletException {}

}

----------------------------------------------------------------------

public class LogonValidateFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain) throws IOException, ServletException {
/*
* 判断用户是否登录(即判断session中是否存在名为user的对象),如果没有登录,将用户的浏览器重定向到/user/logon.do
*
* WebUtil.getBuyer(HttpServletRequest request)
*/
HttpServletRequest request = (HttpServletRequest) req;
Buyer buyer = WebUtil.getBuyer(request);
if(buyer == null){
HttpServletResponse response = (HttpServletResponse)res;
response.sendRedirect("/user/logon.do");
return;
}
filterChain.doFilter(req, res);
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}

----------------------------------------------------------------------------

public class SetCodeFilter implements Filter {

public void destroy() {}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterchain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
req.setCharacterEncoding("UTF-8");
filterchain.doFilter(request, response);
}

public void init(FilterConfig config) throws ServletException {
ConvertUtils.register(new DateConverter(), Date.class);
ConvertUtils.register(new SexConverter(), Sex.class);
ConvertUtils.register(new GenderConverter(), Gender.class);
ConvertUtils.register(new DeliverWayConverter(), DeliverWay.class);
ConvertUtils.register(new PaymentWayConverter(), PaymentWay.class);
ConvertUtils.register(new OrderStateConverter(), OrderState.class);
ConvertUtils.register(new SystemPrivilegePKConverter(), SystemPrivilegePK.class);

try{
Properties prop = new Properties();
prop.put("runtime.log", config.getServletContext().getRealPath("/WEB-INF/log/velocity.log"));
prop.put("file.resource.loader.path", config.getServletContext().getRealPath("/WEB-INF/vm"));
prop.put("input.encoding", "UTF-8");
prop.put("output.encoding", "UTF-8");
Velocity.init(prop);
}catch( Exception e ){
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: