Nearby lessons

16 of 34

Servlet - Request Wrappers

📌 What You Will Learn
  • Understand Demo Program for Request Wrapper
  • See complete working code examples

Request Wrappers is an essential part of the Java Servlet technology. This lesson explains Demo Program for Request Wrapper, CustomizedRequest.java and BadWordFilter.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Demo Program for Request Wrapper

login.html:

Demo Program for Request Wrapper

Example02
Loading editor…

CustomizedRequest.java

Example03
Loading editor…

BadWordFilter.java

Example04
Loading editor…

TargetServlet.java

web.xml:

Example05
Loading editor…

TargetServlet.java

Flow of Program-Execution:

Enter Any Word: 1 23

Submit

Login.htmlTarget Servlet

5 4

Backward Filter

End user entered the word and click the submit button.

Filter creates a CustomizedRequest object by using wrapper class.

Filter forwards that customized request object to the TargetServlet instead of original request.

Within the servlet if we are performing any operation o the request object,our own customized

behaviour will be reflected.

TargetServlet prepares the response and forwards to Filter.

Filter forwards that response inturn to the end user.

Note:

We are not required to configure anything related to wrapper in web.xml.

Conclusions:

  • Filter object will be created by web container automatically. For this web container always calls

public no-arg constructor.Hence every Filter class should compulsory contains public no-arg

constructor. It may be explicitly provided by programmer or default constructor generated by

compiler.

  • Filter object will be created automatically by the web container at the time of application

deployment or at the time of server startup. Hence <load-on-startup> is not required for the

filters.

  • Usage of filter is nothing but following

Intercepting Filter Design Pattern.Hence it is recommended to use Filters concept in our

application.

  • Usage of Wrappers is nothing but following Decorator design pattern. Hence it is

recommended to use Wrappers in our application.

@WebFilter Annotation in Servlet 3.0V:

It is the replacement for filter configurations in web.xml

import javax.servlet.annotation.*;

@WebFilter(filterName="DemoFilter", urlPatterns="/test")

public class DemoFilter implements Filter

{

....

}

Example06
Loading editor…
📝 Key Takeaways
  • Key ideas of Servlet - Request Wrappers explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.