如何为一个特定的 servlet 应用过滤器?
Posted
技术标签:
【中文标题】如何为一个特定的 servlet 应用过滤器?【英文标题】:How to apply filter for one particular servlet? 【发布时间】:2012-11-10 15:19:27 【问题描述】:在聊天中,我的应用程序有五个 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());
提前谢谢....
【问题讨论】:
为什么不将相同的路径应用于您的过滤器作为您的 Servlet ? 感谢您的回复...你能给我密码吗? 您的 servlet 路径将在 web.xml 中,类似于:您还可以通过引用 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>
【讨论】:
很好,我不知道! 我喜欢这种方法,因为它更直接。【参考方案2】:您可以更改过滤器路径:
将<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>
【讨论】:
以上是关于如何为一个特定的 servlet 应用过滤器?的主要内容,如果未能解决你的问题,请参考以下文章
哪些特定用例需要 Struts2 之外但在同一个 Web 应用程序中的 servlet 过滤器?
如何为 tortoise svn 中的日志应用多个过滤器(例如修订号、文件名、开发人员/编码人员)?