首页>代码>深入浅出Struts2书籍源码>/深入浅出Struts2源码/app01b/WEB-INF/src/app01b/FilterDispatcher.java
package app01b;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FilterDispatcher implements Filter {
    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, 
            ServletResponse response, FilterChain filterChain) 
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String uri = req.getRequestURI();
        System.out.println(uri);
        /*
         * uri is in the form of /contextName/resourceName, 
         * for example /app01b/Product_input.action 
         * However, in the case of a default context, 
         * the context name is empty, and uri has this form
         * /resourceName, e.g.: /Product_input.action
         */
        if (uri.endsWith(".action")) {
            // action processing
            int lastIndex = uri.lastIndexOf("/");
            String action = uri.substring(lastIndex + 1); 
            if (action.equals("Product_input.action")) {
                // do nothing
            } else if (action.equals("Product_save.action")) {
                // instantiate action class
                Product product = new Product();
                // populate action properties
                product.setProductName(request.getParameter("productName"));
                product.setDescription(request.getParameter("description"));
                product.setPrice(request.getParameter("price"));
                // execute action method
                product.save();
                // store action in a scope variable for the view
                request.setAttribute("product", product);
            }

            // forward to a view
            String dispatchUrl = null;
            if (action.equals("Product_input.action")) {
                dispatchUrl = "/jsp/ProductForm.jsp";
            } else if (action.equals("Product_save.action")) {
                dispatchUrl = "/jsp/ProductDetails.jsp";
            }
            if (dispatchUrl != null) {
                RequestDispatcher rd = request
                        .getRequestDispatcher(dispatchUrl);
                rd.forward(request, response);
            }
        } else if (uri.indexOf("/css/") != -1
                && req.getHeader("referer") == null) {
            res.sendError(HttpServletResponse.SC_FORBIDDEN);
        } else {
            // other static resources, let it through
            filterChain.doFilter(request, response);
        }
    }
}
最近下载更多
songweifly  LV1 2019年6月16日
朱云飞  LV13 2018年10月20日
zz12534  LV12 2018年7月6日
z924931408  LV25 2018年5月23日
676323640  LV4 2017年8月29日
knife037  LV1 2017年2月14日
dhddxp19780109-1  LV1 2016年10月17日
1044775980  LV1 2016年10月1日
穆尼尔丁  LV12 2016年5月26日
wujixing909  LV2 2016年2月18日
最近浏览更多
你脚上银铃响了  LV1 2021年10月27日
liuhongbing123  LV5 2020年7月13日
lsqhlj  LV1 2020年6月15日
songweifly  LV1 2019年6月16日
花花yayayaya  LV2 2019年6月13日
15101081698  LV3 2019年3月10日
789aaa  LV2 2019年1月2日
pengboooo  LV12 2018年12月22日
SDGWGs  LV1 2018年11月14日
zhaolihuiziyu  LV1 2018年10月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友