Reputation: 55
I am using a Filter
and the doFilter
-Function. I would like to know which site is being requested. Some function giving me something like /firstDirectoryInWebDirectory/lala.jsp
would be perfect.
What is the way to go here? Using the context will be the right way, but I can't find any good method :-(
Thanks
Upvotes: 3
Views: 11679
Reputation: 401
When I just started learning Java EE, it was casual to mix-up ServletRequest and HttpServletRequest methods. As Bozho said, type casting is necessary to access required methods.
This code will log access time and IP when the servlet named "Test" will be requested:
if (((HttpServletRequest)request).getServletPath().equals("/Test")){
String IP = request.getRemoteAddr();
System.out.println("Test Servlet:: Logged IP "+ IP + ", Time :" + new Date().toString());
}
filterChain.doFilter(request,response);
Basically, the request object is casted from a generic request into HTTP Request and only then the return String of getServletPath() can be compared to whatever you want ("/Test" or "/firstDirectoryInWebDirectory/lala.jsp" or "whatever.html").
Upvotes: 0
Reputation: 597116
request.getRequestURI()
should return the part of the URL after the domain.
From there, you can strip the request.getContextPath()
(You'd have to cast the ServletRequest
to HttpServletRequest
first)
Upvotes: 13