web-web配置(1)

1、web的配置:
(1)、设置错误跳转界面:
这里写图片描述
(2)、字符串编码配置:

 <!-- 字符集编码配置 -->
  <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

(3)、配置serlvet 拦截请求脱离struct2之外

  <filter>
        <filter-name>redisp</filter-name>
        <filter-class>yamadv.com.filter.ReDispatcherFilter</filter-class>
        <init-param>
            <param-name>includeServlets</param-name>
            <param-value>FileImageUploadServlet,uploadProcessorServlet</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>redisp</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

/// 该配置集合的类(实现filter):
实现多个servet在web.xml中的配置

public class ReDispatcherFilter implements Filter {

    public void destroy() {}

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        String target = request.getRequestURI();
        target = target.lastIndexOf("?") > 0 ? target.substring(
                target.lastIndexOf("/") + 1,
                target.lastIndexOf("?") - target.lastIndexOf("/")) : target
                .substring(target.lastIndexOf("/") + 1);

        System.out.println(target);
        if (this.includes.contains(target)) {
            RequestDispatcher rdsp = request.getRequestDispatcher(target);

            System.out.println("go..............." + rdsp);
            rdsp.forward(req, resp);
        } else{
            chain.doFilter(req, resp);
        }

    }

    private ArrayList<String> includes = new ArrayList<String>();

    public void init(FilterConfig config) throws ServletException {

        this.includes.addAll(Arrays.asList(config.getInitParameter(
                "includeServlets").split(",")));

    }

}

//要注册的servet(上传文件到服务器):

<!-- 注册 FileImageUploadServlet 的servlet -->
   <servlet>
        <servlet-name>FileImageUploadServlet</servlet-name>
        <servlet-class>yamadv.com.upload.FileImageUploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileImageUploadServlet</servlet-name>
        <url-pattern>/FileImageUploadServlet</url-pattern>
    </servlet-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值