热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javax.servlet.ServletRequest.getAttribute()方法的使用及代码示例

本文整理了Java中javax.servlet.ServletRequest.getAttribute()方法的一些代码示例,展示了ServletReque

本文整理了Java中javax.servlet.ServletRequest.getAttribute()方法的一些代码示例,展示了ServletRequest.getAttribute()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServletRequest.getAttribute()方法的具体详情如下:
包路径:javax.servlet.ServletRequest
类名称:ServletRequest
方法名:getAttribute

ServletRequest.getAttribute介绍

[英]Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using ServletRequest#setAttribute. This allows information to be embedded into a request before a RequestDispatcher call.

Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.
[中]以Objectnull的形式返回命名属性的值(如果不存在给定名称的属性)。
属性可以通过两种方式设置。servlet容器可以设置属性,以提供有关请求的自定义信息。例如,对于使用HTTPS发出的请求,属性javax.servlet.request.X509Certificate可用于检索有关客户端证书的信息。还可以使用ServletRequest#setAttribute通过编程设置属性。这允许在RequestDispatcher调用之前将信息嵌入到请求中。
属性名称应遵循与包名称相同的约定。此规范保留与java.*javax.*sun.*匹配的名称。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
* Expose the specified request attribute if not already present.
* @param request current servlet request
* @param name the name of the attribute
* @param value the suggested value of the attribute
*/
private static void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) {
if (request.getAttribute(name) == null) {
request.setAttribute(name, value);
}
}

代码示例来源:origin: org.springframework/spring-web

/**
* Expose the specified request attribute if not already present.
* @param request current servlet request
* @param name the name of the attribute
* @param value the suggested value of the attribute
*/
private static void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) {
if (request.getAttribute(name) == null) {
request.setAttribute(name, value);
}
}

代码示例来源:origin: spring-projects/spring-framework

/**
* Obtain the {@link WebAsyncManager} for the current request, or if not
* found, create and associate it with the request.
*/
public static WebAsyncManager getAsyncManager(ServletRequest servletRequest) {
WebAsyncManager asyncManager = null;
Object asyncManagerAttr = servletRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE);
if (asyncManagerAttr instanceof WebAsyncManager) {
asyncManager = (WebAsyncManager) asyncManagerAttr;
}
if (asyncManager == null) {
asyncManager = new WebAsyncManager();
servletRequest.setAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, asyncManager);
}
return asyncManager;
}

代码示例来源:origin: org.springframework/spring-web

/**
* Obtain the {@link WebAsyncManager} for the current request, or if not
* found, create and associate it with the request.
*/
public static WebAsyncManager getAsyncManager(ServletRequest servletRequest) {
WebAsyncManager asyncManager = null;
Object asyncManagerAttr = servletRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE);
if (asyncManagerAttr instanceof WebAsyncManager) {
asyncManager = (WebAsyncManager) asyncManagerAttr;
}
if (asyncManager == null) {
asyncManager = new WebAsyncManager();
servletRequest.setAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, asyncManager);
}
return asyncManager;
}

代码示例来源:origin: jersey/jersey

@Override
public void forward(final ServletRequest request, final ServletResponse response) throws ServletException, IOException {
final Object oldIt = request.getAttribute(MODEL_ATTRIBUTE_NAME);
final Object oldResolvingClass = request.getAttribute(RESOLVING_CLASS_ATTRIBUTE_NAME);
request.setAttribute(RESOLVING_CLASS_ATTRIBUTE_NAME, viewable.getResolvingClass());
request.setAttribute(OLD_MODEL_ATTRIBUTE_NAME, viewable.getModel());
request.setAttribute(MODEL_ATTRIBUTE_NAME, viewable.getModel());
request.setAttribute(BASE_PATH_ATTRIBUTE_NAME, basePath);
request.setAttribute(REQUEST_ATTRIBUTE_NAME, request);
request.setAttribute(RESPONSE_ATTRIBUTE_NAME, response);
dispatcher.forward(request, response);
request.setAttribute(RESOLVING_CLASS_ATTRIBUTE_NAME, oldResolvingClass);
request.setAttribute(MODEL_ATTRIBUTE_NAME, oldIt);
}

代码示例来源:origin: jenkinsci/jenkins

/**
* Get a crumb value based on user specific information in the request.
* @param request
*/
public String getCrumb(ServletRequest request) {
String crumb = null;
if (request != null) {
crumb = (String) request.getAttribute(CRUMB_ATTRIBUTE);
}
if (crumb == null) {
crumb = issueCrumb(request, getDescriptor().getCrumbSalt());
if (request != null) {
if ((crumb != null) && crumb.length()>0) {
request.setAttribute(CRUMB_ATTRIBUTE, crumb);
} else {
request.removeAttribute(CRUMB_ATTRIBUTE);
}
}
}
return crumb;
}

代码示例来源:origin: spring-projects/spring-framework

boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);

代码示例来源:origin: apache/incubator-druid

if (request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT) != null) {
filterChain.doFilter(request, response);
return;
request.setAttribute(
AuthConfig.DRUID_AUTHENTICATION_RESULT,
new AuthenticationResult(clientPrincipal, authorizerName, name, null)

代码示例来源:origin: spring-projects/spring-session

HttpServletResponse httpRespOnse= (HttpServletResponse) response;
boolean hasAlreadyFilteredAttribute = request
.getAttribute(this.alreadyFilteredAttributeName) != null;
request.setAttribute(this.alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);

代码示例来源:origin: geoserver/geoserver

(AuthenticationCachingFilter) GeoServerCompositeFilter.this,
(HttpServletRequest) request);
if (cacheKey != null) request.setAttribute(CACHE_KEY_ATTRIBUTE, cacheKey);
Authentication postAuthentication =
SecurityContextHolder.getContext().getAuthentication();
String cacheKey = (String) request.getAttribute(CACHE_KEY_ATTRIBUTE);
if (postAuthentication != null && cacheKey != null) {
Integer idleSecs = (Integer) request.getAttribute(CACHE_KEY_IDLE_SECS);
Integer liveSecs = (Integer) request.getAttribute(CACHE_KEY_LIVE_SECS);
request.setAttribute(CACHE_KEY_ATTRIBUTE, null);
request.setAttribute(CACHE_KEY_IDLE_SECS, null);
request.setAttribute(CACHE_KEY_LIVE_SECS, null);

代码示例来源:origin: org.springframework/spring-web

boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);

代码示例来源:origin: cloudfoundry/uaa

}catch (Exception x) {
logger.error("Uncaught Exception:", x);
if (req.getAttribute("javax.servlet.error.exception") == null) {
req.setAttribute("javax.servlet.error.exception", x);

代码示例来源:origin: apache/shiro

throws ServletException, IOException {
String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
if ( request.getAttribute(alreadyFilteredAttributeName) != null ) {
log.trace("Filter '{}' already executed. Proceeding without invoking this filter.", getName());
filterChain.doFilter(request, response);
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);

代码示例来源:origin: openzipkin/brave

TraceContext cOntext= (TraceContext) request.getAttribute(TraceContext.class.getName());
if (context != null) {
request.setAttribute(SpanCustomizer.class.getName(), span.customizer());
request.setAttribute(TraceContext.class.getName(), span.context());

代码示例来源:origin: nutzam/nutz

Integer mark = (Integer) req.getAttribute(markKey);
if (mark != null) {
req.setAttribute(markKey, mark+1);
} else {
req.setAttribute(markKey, 0);
req.removeAttribute(markKey);
} else {
req.setAttribute(markKey, mark - 1);

代码示例来源:origin: haraldk/TwelveMonkeys

/**
* If request is filtered, returns true, otherwise marks request as filtered
* and returns false.
* A return value of false, indicates that the filter has not yet run.
* A return value of true, indicates that the filter has run for this
* request, and processing should not continue.
*


* Note that the method will mark the request as filtered on first
* invocation.
*


* @see #ATTRIB_RUN_ONCE_EXT
* @see #ATTRIB_RUN_ONCE_VALUE
*
* @param pRequest the servlet request
* @return {@code true} if the request is already filtered, otherwise
* {@code false}.
*/
private boolean isRunOnce(final ServletRequest pRequest) {
// If request already filtered, return true (skip)
if (pRequest.getAttribute(attribRunOnce) == ATTRIB_RUN_ONCE_VALUE) {
return true;
}
// Set attribute and return false (continue)
pRequest.setAttribute(attribRunOnce, ATTRIB_RUN_ONCE_VALUE);
return false;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-web

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
if (request.getAttribute(ALREADY_FILTERED_ATTRIBUTE) != null) {
chain.doFilter(request, response);
} else {
request.setAttribute(ALREADY_FILTERED_ATTRIBUTE, Boolean.TRUE);
try {
this.initializer.setLoggerContext();
chain.doFilter(request, response);
} finally {
this.initializer.clearLoggerContext();
}
}
}

代码示例来源:origin: awslabs/aws-serverless-java-container

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
Object apiGwCOntext= servletRequest.getAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY);
if (apiGwCOntext== null) {
log.warn("API Gateway context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
if (!AwsProxyRequestContext.class.isAssignableFrom(apiGwContext.getClass())) {
log.warn("API Gateway context object is not of valid type");
filterChain.doFilter(servletRequest, servletResponse);
}
AwsProxyRequestContext ctx = (AwsProxyRequestContext)apiGwContext;
if (ctx.getIdentity() == null) {
log.warn("Identity context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
String cognitoIdentityId = ctx.getIdentity().getCognitoIdentityId();
if (cognitoIdentityId == null || "".equals(cognitoIdentityId.trim())) {
log.warn("Cognito identity id in request is null");
}
servletRequest.setAttribute(COGNITO_IDENTITY_ATTRIBUTE, cognitoIdentityId);
filterChain.doFilter(servletRequest, servletResponse);
}

代码示例来源:origin: awslabs/aws-serverless-java-container

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
Object apiGwCOntext= servletRequest.getAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY);
if (apiGwCOntext== null) {
log.warn("API Gateway context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
if (!AwsProxyRequestContext.class.isAssignableFrom(apiGwContext.getClass())) {
log.warn("API Gateway context object is not of valid type");
filterChain.doFilter(servletRequest, servletResponse);
}
AwsProxyRequestContext ctx = (AwsProxyRequestContext)apiGwContext;
if (ctx.getIdentity() == null) {
log.warn("Identity context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
String cognitoIdentityId = ctx.getIdentity().getCognitoIdentityId();
if (cognitoIdentityId == null || "".equals(cognitoIdentityId.trim())) {
log.warn("Cognito identity id in request is null");
}
servletRequest.setAttribute(COGNITO_IDENTITY_ATTRIBUTE, cognitoIdentityId);
filterChain.doFilter(servletRequest, servletResponse);
}

代码示例来源:origin: awslabs/aws-serverless-java-container

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
Object apiGwCOntext= servletRequest.getAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY);
if (apiGwCOntext== null) {
log.warn("API Gateway context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
if (!AwsProxyRequestContext.class.isAssignableFrom(apiGwContext.getClass())) {
log.warn("API Gateway context object is not of valid type");
filterChain.doFilter(servletRequest, servletResponse);
}
AwsProxyRequestContext ctx = (AwsProxyRequestContext)apiGwContext;
if (ctx.getIdentity() == null) {
log.warn("Identity context is null");
filterChain.doFilter(servletRequest, servletResponse);
}
String cognitoIdentityId = ctx.getIdentity().getCognitoIdentityId();
if (cognitoIdentityId == null || "".equals(cognitoIdentityId.trim())) {
log.warn("Cognito identity id in request is null");
}
servletRequest.setAttribute(COGNITO_IDENTITY_ATTRIBUTE, cognitoIdentityId);
filterChain.doFilter(servletRequest, servletResponse);
}

推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
author-avatar
花琦1979
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有