Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Example of HttpServletRequestWrapper class in Servlet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.77k
    Comment on it

    HttpServletRequestWrapper Class : In JavaEE everything is in the form of request and response. User sends the request and server do the response. Now the question is what if we want to change the request data or wanna override the default request methods ? J2EE provides us the feature to edit or modify the request before processing it.

    J2EE have the HttpServletRequestWrapper class to override the HttpServlerRequest methods. We can edit request headers, request data etc. using the HttpServletRequestWrapper class.

    Following is the example of HttpServletRequestWrapper class, Here we will create a class which will extend the HttpServletRequestWrapper class and override it's methods and then we will pass this request to further process.

    Example of HttpServletRequestWrapper class: Here is simple code example of HttpServletRequestWrapper class.

    MyRequestWrapper.java

    public class MyRequestWrapper extends HttpServletRequestWrapper {
        // costructor
        public MyRequestWrapper(HttpServletRequest request) {
            super(request);
            this.request = request;
        }
    
        @Override
        public HttpSession getSession() {
            return this.getSession(true);
        }
    }
    

    AppFilter.java

    @WebFilter("/*")
    public class AppFilter implements Filter {
        @Override 
        public void doFilter(ServletRequest _request,ServletResponse _response,FilterChain chain) throws IOException, ServletException {
      HttpServletRequestWrapper requestWrapper=new MyRequestWrapper((HttpServletRequest)request);
      final HttpServletResponse response=(HttpServletResponse)_response;
      String mimeType=request.getContentType();
     if(mimeType==null)
        response.setContentType("text/plain; charset=utf-8");
     else if(mimeType.startsWith("application/xml"))
        response.setContentType("text/xml; charset=utf-8");
     else if(mimeType.startsWith("text/html"))
        response.setContentType("text/html; charset=utf-8");
    
      chain.doFilter(requestWrapper, response);
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {}
    @Override
    public void destroy() {}
    }
    

    In the above example you can see that we can override the methods of request or we can change the request and response at the time of filtering.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: