本文整理汇总了Java中javax.servlet.ServletRequest.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java ServletRequest.setAttribute方法的具体用法?Java ServletRequest.setAttribute怎么用?Java ServletRequest.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletRequest
的用法示例。
在下文中一共展示了ServletRequest.setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSessionId
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
// 如果参数中包含“__sid”参数,则使用此sid会话。 例如:http://localhost/project?__sid=xxx&__cookie=true
String sid = request.getParameter("__sid");
if (StringUtils.isNotBlank(sid)) {
// 是否将sid保存到cookie,浏览器模式下使用此参数。
if (WebUtils.isTrue(request, "__cookie")) {
HttpServletRequest rq = (HttpServletRequest) request;
HttpServletResponse rs = (HttpServletResponse) response;
Cookie template = getSessionIdCookie();
Cookie cookie = new SimpleCookie(template);
cookie.setValue(sid);
cookie.saveTo(rq, rs);
}
// 设置当前session状态
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, ShiroHttpServletRequest.URL_SESSION_ID_SOURCE); // session来源与url
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sid);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
return sid;
} else {
return super.getSessionId(request, response);
}
}
示例2: getThrowable
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Returns the value of the javax.servlet.error.exception request attribute
* value, if present, otherwise the value of the
* javax.servlet.jsp.jspException request attribute value.
*
* This method is called at the beginning of the generated servlet code for
* a JSP error page, when the "exception" implicit scripting language
* variable is initialized.
*/
public static Throwable getThrowable(ServletRequest request) {
Throwable error = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
if (error == null) {
error = (Throwable) request.getAttribute(PageContext.EXCEPTION);
if (error != null) {
/*
* The only place that sets JSP_EXCEPTION is
* PageContextImpl.handlePageException(). It really should set
* SERVLET_EXCEPTION, but that would interfere with the
* ErrorReportValve. Therefore, if JSP_EXCEPTION is set, we need
* to set SERVLET_EXCEPTION.
*/
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, error);
}
}
return error;
}
示例3: handleRequest
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
SSLSessionInfo ssl = exchange.getConnection().getSslSessionInfo();
if (ssl != null) {
request.setAttribute("javax.servlet.request.cipher_suite", ssl.getCipherSuite());
request.setAttribute("javax.servlet.request.key_size", getKeyLength(ssl.getCipherSuite()));
request.setAttribute("javax.servlet.request.ssl_session_id", ssl.getSessionId());
X509Certificate[] certs = getCerts(ssl);
if (certs != null) {
request.setAttribute("javax.servlet.request.X509Certificate", certs);
}
}
next.handleRequest(exchange);
}
示例4: getThrowable
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Returns the value of the javax.servlet.error.exception request
* attribute value, if present, otherwise the value of the
* javax.servlet.jsp.jspException request attribute value.
*
* This method is called at the beginning of the generated servlet code
* for a JSP error page, when the "exception" implicit scripting language
* variable is initialized.
*/
public static Throwable getThrowable(ServletRequest request) {
Throwable error = (Throwable) request.getAttribute(
RequestDispatcher.ERROR_EXCEPTION);
if (error == null) {
error = (Throwable) request.getAttribute(PageContext.EXCEPTION);
if (error != null) {
/*
* The only place that sets JSP_EXCEPTION is
* PageContextImpl.handlePageException(). It really should set
* SERVLET_EXCEPTION, but that would interfere with the
* ErrorReportValve. Therefore, if JSP_EXCEPTION is set, we
* need to set SERVLET_EXCEPTION.
*/
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, error);
}
}
return error;
}
示例5: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Time the processing that is performed by all subsequent filters in the
* current filter stack, including the ultimately invoked servlet.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Store ourselves as a request attribute (if requested)
if (attribute != null)
request.setAttribute(attribute, this);
// Time and log the subsequent processing
long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
filterConfig.getServletContext().log
(this.toString() + ": " + (stopTime - startTime) +
" milliseconds");
}
示例6: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* This {@code doFilter} implementation stores a request attribute for
* "already filtered", proceeding without filtering again if the attribute is already
* there.
* @param request the request
* @param response the response
* @param filterChain the filter chain
* @throws ServletException if request is not HTTP request
* @throws IOException in case of I/O operation exception
*/
public final void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
if (!(request instanceof HttpServletRequest)
|| !(response instanceof HttpServletResponse)) {
throw new ServletException(
"OncePerRequestFilter just supports HTTP requests");
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
boolean hasAlreadyFilteredAttribute = request
.getAttribute(this.alreadyFilteredAttributeName) != null;
if (hasAlreadyFilteredAttribute) {
// Proceed without invoking this filter...
filterChain.doFilter(request, response);
}
else {
// Do invoke this filter...
request.setAttribute(this.alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);
}
finally {
// Remove the "already filtered" request attribute for this request.
request.removeAttribute(this.alreadyFilteredAttributeName);
}
}
}
示例7: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// retrieve userId and set
if (request instanceof HttpServletRequest) {
Principal principal = ((HttpServletRequest) request).getUserPrincipal();
if (principal != null) {
String ppal = principal.getName();
if (hashAlgorithm == null || "none".equalsIgnoreCase(hashAlgorithm)) {
// no hash
} else if ("hashcode".equalsIgnoreCase(hashAlgorithm)) {
// simply hashcode
ppal = Strings.padStart(Integer.toHexString(ppal.hashCode()), 8, '0');
} else {
// hexadecimal hash
try {
MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
ppal = BaseEncoding.base16().encode(digest.digest(ppal.getBytes()));
} catch (NoSuchAlgorithmException e) {
throw new ServletException(e);
}
}
// add to MDC and request attribute
MDC.put(mdcName, ppal);
request.setAttribute(attributeName, ppal);
}
}
try {
chain.doFilter(request, response);
} finally {
MDC.remove(mdcName);
}
}
示例8: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain ) throws IOException, ServletException {
if (filterConfig==null) return;
if (request.getAttribute("TimeStamp")==null)
request.setAttribute("TimeStamp", new Double(JProf.currentTimeSec()));
try {
// Process request
chain.doFilter(request,response);
_BaseRootDAO.closeCurrentThreadSessions();
} catch (Throwable ex) {
_BaseRootDAO.rollbackCurrentThreadSessions();
if (ex instanceof ServletException) throw (ServletException)ex;
if (ex instanceof IOException) throw (IOException)ex;
if (ex instanceof RuntimeException) throw (RuntimeException)ex;
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
示例9: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Time the processing that is performed by all subsequent filters in the
* current filter stack, including the ultimately invoked servlet.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Store ourselves as a request attribute (if requested)
if (attribute != null)
request.setAttribute(attribute, this);
// Time and log the subsequent processing
long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
long stopTime = System.currentTimeMillis();
filterConfig.getServletContext().log
(this.toString() + ": " + (stopTime - startTime) +
" milliseconds");
}
示例10: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* This {@code doFilter} implementation stores a request attribute for
* "already filtered", proceeding without filtering again if the
* attribute is already there.
*
* @see #getAlreadyFilteredAttributeName
* @see #doFilterInternal
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
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);
} else {
if (!isEnabled(request, response)) {
log.debug("Filter '{}' is not enabled for the current request. Proceeding without invoking this filter.",
getName());
filterChain.doFilter(request, response);
} else {
// Do invoke this filter...
log.trace("Filter '{}' not yet executed. Executing now.", getName());
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
try {
// TODO Maybe do some checks before cast?
doFilterInternal((HttpServletRequest) request, (HttpServletResponse) response, filterChain);
} finally {
// Once the request has finished, we're done and we don't
// need to mark as 'already filtered' any more.
request.removeAttribute(alreadyFilteredAttributeName);
}
}
}
}
示例11: exposeRequestAttributes
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Expose the given Map as request attributes, using the keys as attribute names
* and the values as corresponding attribute values. Keys need to be Strings.
* @param request current HTTP request
* @param attributes the attributes Map
*/
public static void exposeRequestAttributes(ServletRequest request, Map<String, ?> attributes) {
Assert.notNull(request, "Request must not be null");
Assert.notNull(attributes, "Attributes Map must not be null");
for (Map.Entry<String, ?> entry : attributes.entrySet()) {
request.setAttribute(entry.getKey(), entry.getValue());
}
}
示例12: fireRequestInitEvent
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
public boolean fireRequestInitEvent(ServletRequest request) {
Object instances[] = getApplicationEventListeners();
if ((instances != null) && (instances.length > 0)) {
ServletRequestEvent event =
new ServletRequestEvent(getServletContext(), request);
for (int i = 0; i < instances.length; i++) {
if (instances[i] == null)
continue;
if (!(instances[i] instanceof ServletRequestListener))
continue;
ServletRequestListener listener =
(ServletRequestListener) instances[i];
try {
listener.requestInitialized(event);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
getLogger().error(sm.getString(
"standardContext.requestListener.requestInit",
instances[i].getClass().getName()), t);
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
return false;
}
}
}
return true;
}
示例13: onLoginFailure
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token,
AuthenticationException e, ServletRequest request,
ServletResponse response) {
request.setAttribute("remainLoginAttempt", getAccountLocked(getUsername(request)));
return super.onLoginFailure(token, e, request, response);
}
示例14: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
/**
* Filter implementation
* <ul>
* <li>checks whether the current request has an attached request id,
* <li>if not, tries to get one from request headers (implements end-to-end
* callflow traceability),
* <li>if not, generates one
* <li>attaches it to the request (as an attribute) and to the {@link MDC}
* context.
* </ul>
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// checks whether the current request has an attached request id
String reqId = (String) request.getAttribute(attributeName);
if (reqId == null) {
// retrieve id from request headers
if (request instanceof HttpServletRequest) {
reqId = ((HttpServletRequest) request).getHeader(headerName);
}
if (reqId == null) {
// no requestId (either from attributes or headers): generate
// one
reqId = Long.toHexString(System.nanoTime());
}
// attach to request
request.setAttribute(attributeName, reqId);
}
// attach to MDC context
MDC.put(mdcName, reqId);
try {
chain.doFilter(request, response);
} finally {
// remove from MDC context
MDC.remove(mdcName);
}
}
示例15: doFilter
import javax.servlet.ServletRequest; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (logger.isDebugEnabled()) {
logger.debug("AWSXRayServletFilter is beginning to process request: " + request.toString());
}
Segment segment = preFilter(request, response);
try {
chain.doFilter(request, response);
} catch (Exception e) {
if (null != segment) {
segment.addException(e);
}
throw e;
} finally {
if (request.isAsyncStarted()) {
request.setAttribute(AWSXRayServletAsyncListener.ENTITY_ATTRIBUTE_KEY, segment);
try {
request.getAsyncContext().addListener(listener);
} catch (IllegalStateException ise) { // race condition that occurs when async processing finishes before adding the listener
postFilter(request, response);
}
} else {
postFilter(request, response);
}
if (logger.isDebugEnabled()) {
logger.debug("AWSXRayServletFilter is finished processing request: " + request.toString());
}
}
}