Advertisement



< Prev



ServletResponseWrapper class



Today, we are going to create a wrapper class which extends ServletResponseWrapper class(which has implemented ServletResponse interface). Using this wrapper class we will be able to wrap the ServletResponse object and hence we will be able to manipulate, check any response sent by a Servlet to the user.


To do this, we will create a Filter class which will be associated with the requested Servlet and this Filter class will also call the wrapper class to perform the check on any request sent to this Servlet by the user.




Creating a webpage which calls the Servlet


We are creating a webpage which asks the user to enter her/his name, city and click the submit button, which when clicked, will call a Servlet, but before this Servlet is executed, a filter associated with it will be executed( mentioned in the deployment descriptor file(web.xml).

Webpage1.jsp
<html>

<head>
<title> ServletRequest Wrapper Demo </title>
</head>


<body>

<b> Please enter your name : ? </b>
<br/>
<br/>
<br/>

<form action ="MyServ">
Name : <input type = "text"  name = "username" />
City : <input type = "text"  name = "cityname" /> 
<input type = "submit"  name = "submit" />
</form>

</body>
</html>





Creating a Filter class


We are creating a Filter class by implementing the Filter interface and by implementing its three methods


This Filter class creates an object of ServletRequest wrapper class, which is wrapped around the object of ServletRequest. Eventually doFilter() method of FilterChain is called and passed with this request wrapper object as an argument, which makes sure there is a filtering over the request and response object, using the wrapper class.


MyFilter1.java
package decodejava;

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

public class MyFilter1 implements Filter
{

public void init(FilterConfig filterConfig) 
{
}

public void destroy()
{
}


//This method is called each time a client requests for a web resource i.e. preprocessing request
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<b> <i>Filter is filtering the response and passing it to Wrapper class</i> </b> <br/>");


//Calling the constructor of response wrapper class 
ResponseWrapper1 responseWrapper = new ResponseWrapper1(response);	


//This method calls the next filter in the chain
chain.doFilter(request,responseWrapper);
}

}



  • This wrapper class has extended the ServletResponseWrapper(which has implemented ServletResponse interface). This wrapper class and has overridden its getLocale() method of ServletResponseWrapper class, which is invoked when there is a call to getLocale() method of ServletResponse in the requested Servlet. This class creates a ServletResponse wrapper object.


  • ResponseWrapper1.java
    package decodejava;
    
    import java.io.*;
    import javax.servlet.*;
    import java.util.*;
    
    
    
    public class ResponseWrapper1 extends ServletResponseWrapper
    {
    
    
    public ResponseWrapper1(ServletResponse res)
    {
    
    //Calling the ServletResponseWrapper superclass constructor i.e. ServletResponse interface.
    super(res);		
    }
    
    
    public Locale getLocale()
    {	
    System.out.println("Wrapper class called by Filter");
    
    //Calling the superclass method i.e. ServletResponse's getLocale() method.
    Locale loc = super.getLocale(); 
    
    String country = loc.getCountry();
    String language = loc.getLanguage();
    
    
    if(!country.equals("US")) 
    {
    Locale loc1 = new Locale("English, US");
    loc = loc1;
    }
    
    System.out.println(country);
    System.out.println(language);
    
    return loc;
    }
    
    }



  • MyServlet1.java is a Servlet class, which is associated with the filter.


  • MyServlet1.java
    package decodejava;
    
    import javax.servlet.*;
    import java.io.*;
    import java.util.*;
    
    public class MyServlet1 extends GenericServlet
    {
    
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    
    
    System.out.println("Servlet is called");
    
    Locale loc = response.getLocale();
    out.println("Locale set for this application is " + loc.toString());
    }
    
    }



    Advertisement




    Directory Structure of Servlet files




    The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-






    Creating the Deployment Descriptor file


    As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml. So, let's create one and this time, associate our filter class with our Servlet class -


    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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_4_0.xsd"
      version="4.0"
      metadata-complete="true">
    
      <display-name>Welcome tomcat</display-name>
      <description>
         Welcome tomcat
      </description>
    
      
    <filter>
     	<filter-name>Filter1</filter-name>
    	<filter-class>decodejava.MyFilter1</filter-class>
    </filter>
    
    <filter-mapping>
    	<filter-name>Filter1</filter-name>
    	<url-pattern>/MyServ</url-pattern>
    </filter-mapping>
    
    
    
    <servlet>
     	<servlet-name>Servlet</servlet-name>
    	<servlet-class>decodejava.MyServlet1</servlet-class>
    </servlet>
    
    <servlet-mapping>
    	<servlet-name>Servlet</servlet-name>
    	<url-pattern>/MyServ</url-pattern>
    </servlet-mapping>
    
    </web-app>


    In deployment descriptor file, The <filter> has two child tags <filter-name> and <filter-class>.




    <servlet> has two child tags <servlet-name> and <servlet-class> :








    Note









    Setting the classpath


    Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java. Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named servlet-api.jar.

    This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of Tomcat installation folder.

    For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib

    There are two ways to set the classpath -




    Compiling the Servlet class


    After setting the classpath, you need to compile the filter and the Servlet class by entering the command at the folder where you've stored the Servlet class file.

    javac -d WEB-INF/classes ResponseWrapper1.java


    javac -cp ./WEB-INF/classes/;C:/apache-tomcat-9.0.2/lib/servlet-api.jar -d WEB-INF/classes MyFilter1.java


    javac -d WEB-INF/classes MyServlet1.java





    Executing the Servlet


    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement