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
JCode Cell
1 
2<html>
3<body bgcolor=green text=white><center><h1>Durga Software Solutions</h1></center>
4<form action = "/wrapper/test1" >
5<h1>Enter Any Word :<input type=text name=word></h1>
6<input type=submit>
7</form>
8</body>
9</html>
10

CustomizedRequest.java

Example03
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4public class CustomizedRequest extends HttpServletRequestWrapper
5{
6public CustomizedRequest(HttpServletRequest req)
7{
8super(req);// to make original request information available to the Parent class
9}
10public String getParameter(String word)
11{
12String word1 = super.getParameter(word);
13if(word1.equals("JAVA") | word1.equals("SCJP") | word1.equals("SCWCD") | word1.equals("SAT"))
14return "SLEEP";
15else
16return word1;
17}
18}
19

BadWordFilter.java

Example04
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class BadWordFilter implements Filter
6{
7public void init(FilterConfig conf) throws ServletException
8{
9}
10public void doFilter(ServletRequest req,ServletResponse resp,FilterChain fc) throws ServletException,IOException
11{
12CustomizedRequest req1 = new CustomizedRequest((HttpServletRequest)req);
13fc.doFilter(req1,resp);
14}
15public void destroy()
16{
17}
18}
19

TargetServlet.java

web.xml:

Example05
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class TargetServlet extends HttpServlet
6{
7 
8public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
9{
10PrintWriter out = resp.getWriter();
11String word = req.getParameter("word");
12out.println("<h1>Hi your typed word is :"+word+"</h1>");
13}
14}
15

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
JCode Cell
1 
2<web-app>
3<filter>
4<filter-name>BadWordFilter</filter-name>
5<filter-class>BadWordFilter</filter-class>
6</filter>
7<filter-mapping>
8<filter-name>BadWordFilter</filter-name>
9<servlet-name>TargetServlet</servlet-name>
10</filter-mapping>
11 
12<servlet>
13<servlet-name>TargetServlet</servlet-name>
14<servlet-class>TargetServlet</servlet-class>
15</servlet>
16 
17<servlet-mapping>
18<servlet-name>TargetServlet</servlet-name>
19<url-pattern>/test1</url-pattern>
20</servlet-mapping>
21</web-app>
22
📝 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