Java Code Examples for javax.servlet.ServletRequestEvent#getServletRequest()

The following examples show how to use javax.servlet.ServletRequestEvent#getServletRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestContextListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 2
Source File: GeomajasContextListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
	if (servletRequestEvent.getServletRequest() instanceof HttpServletRequest) {
		HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
		ServletRequestAttributes attributes = new ServletRequestAttributes(request);
		request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
		LocaleContextHolder.setLocale(request.getLocale());
		RequestContextHolder.setRequestAttributes(attributes);
	}
}
 
Example 3
Source File: BugsnagServletRequestListener.java    From bugsnag-java with MIT License 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
    trackServletSession();
    ServletRequest servletRequest = servletRequestEvent.getServletRequest();

    if (servletRequest instanceof HttpServletRequest) {
        SERVLET_REQUEST.set((HttpServletRequest) servletRequest);
    }
}
 
Example 4
Source File: RequestListener.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent event) {
    HttpServletRequest request = (HttpServletRequest)event.getServletRequest();
    if (!request.getServletPath().equals("/counter")) {
        ServletContext context = event.getServletContext();
        context.setAttribute("counter", (int)context.getAttribute("counter") + 1);
    }
}
 
Example 5
Source File: RequestContextListener.java    From onboard with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
    if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
        logger.debug("error type: " + requestEvent.getServletRequest().getClass().getName());
        throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletContext());
    }

    HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
    GlobalServiceImpl.setUpHttpSession(request);
}
 
Example 6
Source File: SymphonyServletListener.java    From symphonyx with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(final ServletRequestEvent servletRequestEvent) {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequestEvent.getServletRequest();

    httpServletRequest.setAttribute(Keys.TEMAPLTE_DIR_NAME, Symphonys.get("skinDirName"));

    if (Requests.searchEngineBotRequest(httpServletRequest)) {
        LOGGER.log(Level.DEBUG, "Request made from a search engine[User-Agent={0}]", httpServletRequest.getHeader("User-Agent"));
        httpServletRequest.setAttribute(Keys.HttpRequest.IS_SEARCH_ENGINE_BOT, true);

        return;
    }

    httpServletRequest.setAttribute(Keys.HttpRequest.IS_SEARCH_ENGINE_BOT, false);

    if (StaticResources.isStatic(httpServletRequest)) {
        return;
    }

    // Gets the session of this request
    final HttpSession session = httpServletRequest.getSession();
    LOGGER.log(Level.TRACE, "Gets a session[id={0}, remoteAddr={1}, User-Agent={2}, isNew={3}]",
            new Object[]{session.getId(), httpServletRequest.getRemoteAddr(), httpServletRequest.getHeader("User-Agent"),
                session.isNew()});

    resolveSkinDir(httpServletRequest);
}
 
Example 7
Source File: RequestListener.java    From trimou with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent event) {
    if (event.getServletRequest() instanceof HttpServletRequest) {
        RequestHolder.requestInitialized((HttpServletRequest) event
                .getServletRequest());
    } else {
        throw new IllegalStateException(
                "This extension only supports HTTP Servlet lifecycle");
    }
}
 
Example 8
Source File: MyServletRequestListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent event) {

	HttpServletRequest request = (HttpServletRequest) event.getServletRequest();

	HttpSession session = request.getSession(true);

	// 记录IP地址
	session.setAttribute("ip", request.getRemoteAddr());

	// 记录访问次数,只记录访问 .html, .do, .jsp, .action 的累计次数
	String uri = request.getRequestURI();
	uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());
	request.setAttribute("dateCreated", System.currentTimeMillis());
       String[] suffix = { ".html", ".do", ".jsp", ".action" };
	for (int i = 0; i < suffix.length; i++) {
		if (uri.endsWith(suffix[i])) {
			break;
		}
		if (i == suffix.length - 1) {
			return;
		}
	}

	Integer activeTimes = (Integer) session.getAttribute("activeTimes");

	if (activeTimes == null) {
		activeTimes = 0;
	}

	session.setAttribute("activeTimes", activeTimes + 1);
	logger.debug("IP: {} 请求 {}", request.getRemoteAddr(), uri);
}
 
Example 9
Source File: RequestContextListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 10
Source File: RequestContextListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 11
Source File: SHDRequestListener.java    From kardio with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
    HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
    String endPointName = request.getRequestURI();
    endPointName = endPointName.substring(1);
    endPointName = endPointName.replaceAll("/", "_");
    prometheusMetricService.initializeMetricData(request, endPointName);
}
 
Example 12
Source File: RequestContextListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 13
Source File: SocketServletListener.java    From webChat with Apache License 2.0 4 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
    HttpServletRequest request=(HttpServletRequest) sre.getServletRequest();
    HttpSession session=request.getSession();
    session.setAttribute("ClientIPadd", sre.getServletRequest().getRemoteAddr());//把HttpServletRequest中的IP地址放入HttpSession中,关键字可任取,此处为ClientIP
}
 
Example 14
Source File: MyServletRequestListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent event) {
	HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
	long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");
	logger.debug("{} 请求处理结束, 用时 {} 毫秒", request.getRemoteAddr(), time);
}
 
Example 15
Source File: RequestListener.java    From Spring-Boot-Book with Apache License 2.0 4 votes vote down vote up
@Override
    public void requestDestroyed(ServletRequestEvent arg0) {
        System.out.println("监听器初始化...");
        Integer count = null;//请求数量
        Object requestCount = arg0.getServletContext().getAttribute("requestCount");
        if (requestCount == null) {
            count = 0;
        } else {
            count = Integer.valueOf(requestCount.toString());
        }
        count++;
        System.out.println("当前请求数:" + count.toString());
        arg0.getServletContext().setAttribute("requestCount", count);
        /*用户信息*/





        /*用户信息*/
        HttpServletRequest servletRequest = (HttpServletRequest) arg0.getServletRequest();
        //requestURL
        String requestURL = servletRequest.getRequestURL().toString();
        //queryString
        String queryString = servletRequest.getQueryString();

        RequestLog requestLog = new RequestLog();
        requestLog.setIpaddr(IpUtils.getIpAddr(servletRequest));
        requestLog.setSessionId(servletRequest.getRequestedSessionId());
        requestLog.setReferer(servletRequest.getHeader("Referer"));
        requestLog.setAccept(servletRequest.getHeader("accept"));
        requestLog.setMethod(servletRequest.getMethod());
        requestLog.setUrl(requestURL);
        requestLog.setQuerystring(queryString);
        /*UserAgent 工具类*/
        UserAgent userAgent = UserAgent.parseUserAgentString(servletRequest.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        Version version = browser.getVersion(servletRequest.getHeader("User-Agent"));

        String browserInfo = null;
        try {
            browserInfo = browser.getName() + "/" + version.getVersion();
        } catch (Exception e) {
            e.printStackTrace();
        }

        OperatingSystem os = userAgent.getOperatingSystem();

        /*UserAgent 工具类*/
        requestLog.setOs(os.getName());
        requestLog.setBrowser(browserInfo);
        requestLog.setCreationTimes(System.currentTimeMillis());


/*        SecurityContextImpl securityContextImpl = (SecurityContextImpl) servletRequest.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        requestLog.setLoginName( securityContextImpl.getAuthentication().getName());*/
        requestLogRepository.save(requestLog);
    }
 
Example 16
Source File: MyServletRequestListener.java    From journaldev with MIT License 4 votes vote down vote up
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
	ServletRequest servletRequest = servletRequestEvent.getServletRequest();
	System.out.println("ServletRequest initialized. Remote IP="+servletRequest.getRemoteAddr());
}
 
Example 17
Source File: WeldInitListener.java    From piranha with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
    super.requestInitialized(new ServletRequestEvent(
        sre.getServletContext(), 
        new WeldHttpServletRequest((HttpServletRequest)sre.getServletRequest())));
}
 
Example 18
Source File: SessionsCounterService.java    From live-chat-engine with Apache License 2.0 4 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent event) {
	HttpServletRequest req = (HttpServletRequest) event.getServletRequest();
	String clientIp = getClientIp(req);
	reqIpThreadLocal.set(clientIp);
}
 
Example 19
Source File: RollbarRequestListener.java    From rollbar-java with MIT License 4 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
  if (sre.getServletRequest() instanceof HttpServletRequest) {
    CURRENT_REQUEST.set((HttpServletRequest) sre.getServletRequest());
  }
}
 
Example 20
Source File: SHDRequestListener.java    From kardio with Apache License 2.0 4 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
    HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
    prometheusMetricService.pushMetric(request);
}