本文整理汇总了Java中javax.servlet.ServletRequest类的典型用法代码示例。如果您正苦于以下问题:Java ServletRequest类的具体用法?Java ServletRequest怎么用?Java ServletRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServletRequest类属于javax.servlet包,在下文中一共展示了ServletRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
final String authHeader = request.getHeader("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
ExceptionUtils.createUnauthorizedException("Missing or invalid Authorization header.", res);
return;
}
try {
final String token = authHeader.substring(7); // The part after "Bearer "
final Claims claims =
Jwts.parser().setSigningKey("secretkey").parseClaimsJws(token).getBody();
request.setAttribute("claims", claims);
} catch (final Exception e) {
ExceptionUtils.createUnauthorizedException("Invalid token", res);
return;
}
chain.doFilter(req, res);
}
示例2: setRequest
import javax.servlet.ServletRequest; //导入依赖的package包/类
/**
* Set the request that we are wrapping.
*
* @param request The new wrapped request
*/
@Override
public void setRequest(ServletRequest request) {
super.setRequest(request);
// Initialize the attributes for this request
synchronized (attributes) {
attributes.clear();
Enumeration<String> names = request.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
Object value = request.getAttribute(name);
attributes.put(name, value);
}
}
}
示例3: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.print(getClass().getName());
chain.doFilter(request, response);
}
示例4: send401
import javax.servlet.ServletRequest; //导入依赖的package包/类
private void send401(ServletRequest httpRequest,
HttpServletResponse httpResponse, HttpSession session, boolean asTM) {
// Return 401 error
String clientLocale = httpRequest.getLocale().getLanguage();
if (clientLocale.equals(LOCALE_JA)) {
clientLocale = LOCALE_DEFAULT;
}
httpResponse.setHeader(
"WWW-Authenticate",
"Basic realm=\""
+ Messages.get(clientLocale,
(asTM ? "ui.config.authentication.tm"
: "ui.config.authentication.appadmin"))
+ "\"");
httpResponse.setStatus(401);
session.invalidate();
}
示例5: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String servletPath = req.getServletPath() + req.getPathInfo();
xLogger.fine("Servlet path: ", servletPath);
if (req.getCharacterEncoding() == null) {
request.setCharacterEncoding(Constants.UTF8);
}
if (!(StringUtils.isNotBlank(servletPath) && servletPath.startsWith(MEDIA_ENDPOINT_URL))) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (filterChain != null) {
filterChain.doFilter(request, response);
}
}
示例6: onLoginFailure
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
if (!((HttpServletRequest)request).getRequestURL().toString().endsWith(".json")) {
setFailureAttribute(request, e);
return true;
}
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
String message = e.getClass().getSimpleName();
if ("IncorrectCredentialsException".equals(message)
|| "UnknownAccountException".equals(message)
) {
out.println("{\"code\":-100010,\"info\":\"账号或密码错误\"}");
}else if("ExcessiveAttemptsException".equals(message)){
out.println("{\"code\":-100020,\"info\":\"密码错误次数超过限制,请10分钟后重试!\"}");
}else if("LockedAccountException".equals(message)){
out.println("{\"code\":-100030,\"info\":\"账号已停用!\"}");
} else {
out.println("{\"code\":-100500,\"info\":\"未知错误\"}");
}
out.flush();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
示例7: dispatch
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void dispatch(ServletRequest request, ServletResponse response) throws ServletException, IOException {
if (Globals.IS_SECURITY_ENABLED) {
try {
PrivilegedDispatch dp = new PrivilegedDispatch(request, response);
AccessController.doPrivileged(dp);
} catch (PrivilegedActionException pe) {
Exception e = pe.getException();
if (e instanceof ServletException)
throw (ServletException) e;
throw (IOException) e;
}
} else {
doDispatch(request, response);
}
}
示例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 {
// Get Filter Init Parameters
String replaceStrParam = filterConfig.getInitParameter("replace_string");
String addlCharsParam = filterConfig.getInitParameter("additional_chars");
// Set variables
if(replaceStrParam!=null)
replaceStr = replaceStrParam;
if(addlCharsParam!=null)
addlChars = addlCharsParam;
// Construct allowed characters pattern
String charPattern = "([^" + charsAllowed + addlChars + "]+)(%0A)";
// Instantiate actual filter
RequestXSSFilter rxs = new RequestXSSFilter(
(HttpServletRequest) request, replaceStr, charPattern);
// Process request
chain.doFilter(rxs,response);
}
示例9: setRequest
import javax.servlet.ServletRequest; //导入依赖的package包/类
/**
* Set the request that we are wrapping.
*
* @param request The new wrapped request
*/
public void setRequest(ServletRequest request) {
super.setRequest(request);
// Initialize the attributes for this request
synchronized (attributes) {
attributes.clear();
Enumeration names = request.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
Object value = request.getAttribute(name);
attributes.put(name, value);
}
}
}
示例10: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
/**这个方法完成实际的过滤操作。当客户请求访问与过滤器关联的URL的时候,Servlet过滤器将先执行doFilter方法。FilterChain参数用于访问后续过滤器。*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
//忽略不过滤页面
if(!Statics.ignore_login_page.contains(httpRequest.getServletPath())){
long memberId = StorageUtil.init(httpRequest.getSession()).getLoginMemberId();
if(memberId==ReturnUtil.NOT_LOGIN_CODE){
String url = httpRequest.getScheme()+"://"+httpRequest.getServerName()+":"+httpRequest.getServerPort()+httpRequest.getContextPath();
DataUtil.OutStreamByResponse(httpResponse, "<script type='text/javascript'>top.location.href= '"+url+Statics.Login_Page+"';</script>");
return;
}
}
chain.doFilter(request, response);
// httpRequest.getScheme();//协议类型 http
// httpRequest.getServerName();//域名 mycdsxlc.f3322.net
// httpRequest.getServerPort();//端口号 8989
// httpRequest.getContextPath();//项目名 /Report
// httpRequest.getServletPath();//请求页面 /web/index.jsp
// httpRequest.getQueryString();//参数 null
}
示例11: service
import javax.servlet.ServletRequest; //导入依赖的package包/类
/**
* {@inheritDoc}
* @throws ApplicationContextException if the DispatcherServlet does not
* initialize properly, but the servlet attempts to process a request.
*/
@Override
public void service(final ServletRequest req, final ServletResponse resp)
throws ServletException, IOException {
/*
* Since our container calls only this method and not any of the other
* HttpServlet runtime methods, such as doDelete(), etc., delegating
* this method is sufficient to delegate all of the methods in the
* HttpServlet API.
*/
if (this.initSuccess) {
this.delegate.service(req, resp);
} else {
throw new ApplicationContextException("Unable to initialize application context.");
}
}
示例12: 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");
}
示例13: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("LoggedUser") == null) {
response.sendRedirect(request.getContextPath() + "/index.jsp"); // No logged-in user found, so redirect to login page.
} else {
DynUser currentUser = (DynUser) session.getAttribute("LoggedUser");
if (!currentUser.isAdmin()) {
response.sendRedirect(request.getContextPath() + "/app/dashboard.jsp"); // No admin user found, so redirect to dashboard page.
} else {
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
}
示例14: invoke
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException {
valveContext.invokeNext(request, response);
System.out.println("Header Logger Valve:");
ServletRequest sreq = request.getRequest();
if(sreq instanceof HttpServletRequest){
HttpServletRequest hreq = (HttpServletRequest) sreq;
Enumeration<String> headerNames = hreq.getHeaderNames();
while (headerNames.hasMoreElements()){
String headerName = headerNames.nextElement().toString();
String headerValue = hreq.getHeader(headerName);
System.out.println(headerName + ": " + headerValue);
}
}else {
System.out.println("Not an HTTP Request");
}
System.out.println("-------------------------------------");
}
示例15: doFilter
import javax.servlet.ServletRequest; //导入依赖的package包/类
@Override
public void doFilter(ServletRequest req0, ServletResponse resp0,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req0;
HttpServletResponse response = (HttpServletResponse) resp0;
String authorization = request.getHeader("Authorization");
if (authorization != null) {
String c[] = parseAuthorization(authorization);
if (c != null && credentials.containsKey(c[0])
&& credentials.get(c[0]).equals(c[1])) {
chain.doFilter(request, response);
return;
}
}
response.setHeader("WWW-Authenticate", "Basic realm=\"Restricted\"");
response.sendError(401);
}