简体   繁体   English

Java Servlet将值从过滤器传递到Servlet代码

[英]Java Servlet Pass Value from Filter to Servlet Code

I have a Java HTTP Servlet running in Tomcat. 我有一个在Tomcat中运行的Java HTTP Servlet。 I have a filter that is doing some pre-processing on requests. 我有一个过滤器,正在对请求进行一些预处理。 The filter contains some if/else logic. 过滤器包含一些if / else逻辑。 I want to set a boolean value in my servlet processing code (doGet/doPost) based on which action my filter performed. 我想根据我的过滤器执行的操作在Servlet处理代码(doGet / doPost)中设置一个布尔值。

What is the correct way to pass this information from my filter to my servlet method? 将信息从过滤器传递到servlet方法的正确方法是什么? One idea is to update the request via a setAttribute method, but I am not sure if this is the right way. 一种想法是通过setAttribute方法更新请求,但是我不确定这是否正确。

In your doFilter(ServletRequest request, ServletResponse response, FilterChain chain) method you can use the request object and set an attribute using request.setAttribute method and then get it in your servlet using request.getAttribute . doFilter(ServletRequest request, ServletResponse response, FilterChain chain)方法中,可以使用request对象并使用request.setAttribute方法设置属性,然后使用request.getAttribute在Servlet中获取它。

See docs: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setAttribute(java.lang.String , java.lang.Object) 请参阅文档: http : //docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setAttribute (java.lang.String,java.lang.Object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM