Submitted by heartin on Fri, 03/20/2015 - 12:02
Filters are pluggable classes that stand between the client and a target component (like a servlet or JSP), within a web application. We can do pre or post processing of request/response data while it is coming from client to a servlet or from the servlet back to a client.
Create a Servlet TargetServlet with URL Pattern as “/TargetServlet” and print some content to console from its doGet method. Code for TargetServlet is given in the end.
Now create a filter that will have the same url pattern for the servlet.
Submitted by heartin on Fri, 03/20/2015 - 11:37
Filters are pluggable classes that stand between the client and a target component (like a servlet or JSP), within a web application. We can do pre or post processing of request/response data while it is coming from client to a servlet or from the servlet back to a client. Before filters were introduced in Java EE, we had to use servlets with RequestDispatcher to achieve pre or post processing of request/response data. Filters add a lot of flexibility over servlet for such extra processing which is not directly involved in response creation.
Submitted by heartin on Fri, 03/20/2015 - 09:24
-
What are the three ways in which you can tell the container that your class is a servlet?
-
Compare filters with RequestDispatcher mechanism.
-
Can you include or forward to filters using the RequestDispatcher mechanism?
-
What will happen if you call two RequestDispatcher forwards or includes from within a servlet's doXXX method?
-
Can you use wrappers to have two RequestDispatcher forward requests without any exception?
Submitted by heartin on Thu, 03/19/2015 - 21:38
If you need to extend the functionality of request or response objects, there are wrapper classes that can wrap around current request or response. The wrapper classes implements the required interfaces so that we can pass it to any place that expects those interface implementations. These wrappers will simply delegate the calls to the default container implementation of the request and response interfaces.
Submitted by heartin on Thu, 03/19/2015 - 21:23
-
Forms and parameters
-
Create a servlet and name it as AuthServlet
-
Create an html file
-
Create a form that accepts a name, password and pass it on to the above servlet.
-
The receiving servlet (AuthServlet) should check the name and password against a hard coded list of name-password pairs stored in a HashMap. HashMap should be initialized with some dummy usernames and passwords in the constructor/init method of the servlet.
Pages