如何为一个特定的servlet应用过滤器?

时间:2012-11-22 09:08:03

标签: java servlets servlet-filters web.xml

在聊天我的应用程序中我有五个servlet。我需要为单个servlet应用过滤器。

我的过滤器是,

package com.tps.flexchat.action;

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;

/**
 * Servlet Filter implementation class EncodingFilter
 */
public class EncodingFilter implements Filter {
    private String encoding = "UTF-8";
    private FilterConfig filterConfig;
    /**
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    /**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {

        resp.setCharacterEncoding("encoding");
        resp.setContentType("encoding");
                    req.setCharacterEncoding(encoding);
        chain.doFilter(req, resp);

        System.out.println("filter -------> :"+encoding );
    }

    /**
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
    }

}

web.xml中的映射将是

<filter>
    <display-name>EncodingFilter</display-name>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.tps.flexchat.action.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

我只需要将EncodingFilter过滤器应用于此SendMessageAction servlet,

package com.tps.flexchat.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.genesyslab.platform.webmedia.protocol.FlexChatProtocol;
import com.tps.flexchat.Request.SendMessage;
import com.tps.flexchat.info.ApplicationInfo;
import com.tps.flexchat.info.CustomerInfo;

public class SendMessageAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private String msg;
    private String seckey;
    private String uid;
    private String sessionId;
    private int counter;
    private FlexChatProtocol protocol = null; 
    private SendMessage message;


    public SendMessageAction() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            msg = request.getParameter("message");
            seckey = request.getParameter("securekey");
            uid = request.getParameter("userId");
            sessionId = request.getParameter("sessionId");
            counter = 1;
            protocol = ApplicationInfo.flexProtocol;

            message = new SendMessage();
            message.send(msg, seckey, uid, sessionId, counter, protocol);

            CustomerInfo customer = ApplicationInfo.customerDetails.get(uid);

            out.print(customer.getMessage());

        }

    }

提前致谢....

2 个答案:

答案 0 :(得分:7)

您还可以通过引用servlet名称而不是url模式将过滤器应用于特定的servlet。如果您的servlet声明如下:

<servlet>
    <servlet-name>SendMessageAction</servlet-name>
    <servlet-class>com.tps.flexchat.action.SendMessageAction</servlet-class>
</servlet>

您应该能够使用以下过滤器映射:

<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <servlet-name>SendMessageAction</servlet-name>
</filter-mapping>

答案 1 :(得分:6)

您可以更改过滤器路径:

<url-pattern>更改为与servlet相同的路径

<filter>
    <display-name>EncodingFilter</display-name>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.tps.flexchat.action.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/SendMessageAction/</url-pattern>
</filter-mapping>