热门标签 | 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);
}

推荐阅读
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 标题: ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
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社区 版权所有