Saturday 23 August 2014

HttpServletRequestWrapper

This class provides implementation of the HttpServletRequest interface that can be sub classed by developers wishing to adapt the request to a Servlet.

Example
import javax.servlet.http.*;

public class ReqWrapper extends HttpServletRequestWrapper {
    
    HttpServletRequest request;
    
    ReqWrapper(HttpServletRequest request){
       super(request);
       this.request = request;
    }
   
    public String GetDetails(){
       StringBuilder details = new StringBuilder("RemoteHost :");
       details.append(request.getRemoteHost());
       details.append("<br />Remote Address :").append(request.getRemoteAddr());
       details.append("<br />Remote User :").append(request.getRemoteUser());
       details.append("<br />Remote Port :").append(request.getRemotePort());
       details.append("<br />Server Name:").append(request.getServerName());
       details.append("<br />port :").append(request.getServerPort());
       
       return details.toString();
    }
}

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ReqFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException{
        
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{
        ReqWrapper wrap = new ReqWrapper((HttpServletRequest)request);
        chain.doFilter(wrap, response);
    }
    
    @Override
    public void destroy(){
        
    }
}

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class WrapperEx extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        ReqWrapper wrap = (ReqWrapper)req;
        String details = wrap.GetDetails();
        
        try(PrintWriter out = res.getWriter()){
            res.setContentType("text/html");
            out.println("<html><head></head><body><h1>");
            out.println(details);
            out.println("</h1></body></html");
        }       
    }
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>ReqFilter</filter-name>
        <filter-class>ReqFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ReqFilter</filter-name>
        <url-pattern>/WrapperEx</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>WrapperEx</servlet-name>
        <servlet-class>WrapperEx</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>WrapperEx</servlet-name>
        <url-pattern>/WrapperEx</url-pattern>
    </servlet-mapping>
</web-app>

Run the servlet like below
'http://localhost:8080/servlet/WrapperEx'

Output





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment