I'd like to modify the response object of type HttpServletResponse, I've implemented the filter which extends OncePerrequestFilter base class.
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
os = response.getOutputStream();
}
I'd like to get the response as String, modifying it and updating the HttpServletRequest instance content with my modified String. It is possible ?
Related
I have application that is running on ATG. I have added filter servlet also. While login(using ATGForm), I am passing one parameter. I am able to get that param in DynamoHttpServletRequest. But, after I do forward or redirect to some JSP page, I am not able to get that param in the Filter servlet.
Filter Servlet as below:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
I am not able to get the same param in request. Anything I am missing here?
You can follow the doc for more :
https://docs.oracle.com/cd/E35319_01/Platform.10-2/ATGPlatformProgGuide/html/s0704filterexample01.html
import atg.servlet.ServletUtil;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyFilter
implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
// Get the Dynamo Request/Response Pair
DynamoHttpServletRequest dRequest =
ServletUtil.getDynamoRequest(request);
// Get param value
String paramValue =
(String)dRequest.resolveName("paramName");
// Pass control on to the next filter
chain.doFilter(request,response);
return;
}
}
I'm having trouble redirecting the user in servlet filter.
#WebFilter(value = "/*", initParams = { #WebInitParam(name = "applicationClassName", value = "com.sample.Application"),
#WebInitParam(name="filterMappingUrlPattern", value="/*") })
public class CertFilter extends WicketFilter {
#Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
if(USER_MATCHES_CONDITION){
httpServletResponse.setHeader("Location", "https://google.com");
httpServletResponse.sendRedirect("https://google.com");
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
The request to google.com is done but user is not redirected to there. The request intercepted meaning USER_MATCHES_CONDITION is met at the time when stylesheet is requested. Thus redirection request header is Accept:text/css,*/*;q=0.1. So I get a warning Resource interpreted as Stylesheet but transferred with MIME type text/html. Probably this is the reason why it's not redirecting.
How to intercept random request and redirect it to external url?
EDIT: Even if I do just like this:
#Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
httpServletResponse.sendRedirect("https://google.com");
return;
}
It does not redirect the user... I'm guessing wicket overrides it somehow.
To no-cache web page, in the java controller servlet, I did somthing like this in a method:
public ModelAndView home(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView(ViewConstants.MV_MAIN_HOME);
mav.addObject("testing", "Test this string");
mav.addObject(request);
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
return mav;
}
But this only works for a particular response object. I have many similar methods in a servlet. And I have many servlets too.
If I want to disable cache throughout the application, what should I do?
(I do not want to add above code for every single response object).
Why not do this via a filter?
A filter is an object that can transform the header and content (or both) of a request or response.
...
The main tasks that a filter can perform are as follows:
...
Modify the response headers and data. You do this by providing a customized version of the response.
Just register your Filter (class implementing the Filter interface) and modify your response within the doFilter method.
EDIT: E.g.
#WebFilter("/*")
public class NoCacheFilter implements javax.servlet.Filter {
#Override
public void init(final FilterConfig filterConfig) throws ServletException {
}
#Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
filterChain.doFilter(request, response);
}
#Override
public void destroy() {
}
}
Note that the #WebFilter annotation will require Servlet 3.0, otherwise you can register it via your web.xml. This path of "/*", would apply to any path of your application, but could be narrowed in scope.
Hi please some one help me in this issue.
I have a class which extends HttpServlet and a method called doGet(HttpServletRequest request)
But i dont have a HttpServletResponse .Now i want to respond to a request from a client as im not having response how can i respond to a request.Can i create able to create it dynamically in that class using reflections and is their any other way..My problems pseudo code looks as follows
protected void doGet(HttpServletRequest request)throws ServletException, IOException {//having only request as parameter in doGet()
//Want to respond to a jsp with out having a response
//how can i create a response object if i got a HttpServletResponse response the code looks as follows
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
if(name.equals("James")&& password.equals("abc")){
response.sendRedirect("result.jsp");
}
else{
pw.println("u r not a valid user");
}
}
There is no such method protected void doGet(HttpServletRequest request) throws ServletException, IOException
The method signature is:
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
I have a servlet which performs various business logic. I want to avoid synchronisation like this:
#Override
protected void doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
synchronized (MyServlet.class) {
various();
calls();
and_logic(_req, _resp);
}
}
by making all called methods static and enforcing it like this:
#Override
protected void doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
_doGet(_req, _resp);
}
private static void _doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
various();
calls();
and_logic(_req, _resp);
}
I won't use any static variables and all my method calls are assumed to be thread-safe. Are there any non-obvious drawbacks?
I won't use any static variables and all my method calls are assumed to be thread-safe.
Under these conditions neither you don't need synchronization nor static methods. Just use instance methods of a servlet or some other service class.