Q1.What is the Difference between Filters and Interceptors?
Filter: – A filter as the name suggests is a Java class executed by the servlet container for each incoming HTTP request and for each http response. This way, is possible to manage HTTP incoming requests before they reach the resource, such as a JSP page, a servlet or a simple static page; in the same way, it’s possible to manage HTTP outbound response after resource execution.
- A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses.
- Filters typically do not themselves create a response, but instead provide universal functions that can be “attached” to any type of servlet or JSP page.
- They provide the ability to encapsulate recurring tasks in reusable units. Organized developers are constantly on the lookout for ways to modularize their code.
- Modular code is more manageable and documentable, is easier to debug, and if done well, can be reused in another setting.
Interceptor: – Spring Interceptors are similar to Servlet Filters but they act in Spring Context so are many powerful to manage HTTP Request and Response but they can implement more sophisticated behavior because can access to all Spring context. Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods in conjunction with method invocations or lifecycle events on an associated target class. Common uses of interceptors are logging, auditing, or profiling.
- Interceptor can be defined within a target class as an interceptor method, or in an associated class called an interceptor class.
- Interceptor classes contain methods that are invoked in conjunction with the methods or lifecycle events of the target class.
- Interceptor classes and methods are defined using metadata annotations, or in the deployment descriptor of the application containing the interceptors and target classes.
- Interceptor classes may be targets of dependency injection. Dependency injection occurs when the interceptor class instance is created, using the naming context of the associated target class, and before any @PostConstruct callbacks are invoked.
Q2.Spring interceptor vs servlet filter?
- Using Interceptor we can inject other beans in the interceptor
- Can use more advanced mapping patterns (ant-style)
- You have the target handler object (controller) available, as well as the result ModelAndView
- It is a bean, so you can use AOP with it (althoug that would be rare)
Q3.How to avoid multiple submission of Form to Server?
- Send a redirect after submit, this is known as Post-Redirect-Get (PRG) pattern. This will avoid multiple submits being caused by users pressing F5 on the result page and ignoring the browser warning that the data will be resend, or navigating back and forth by browser back/forward buttons and ignoring the same warning.
- Generate an unique token when the page is requested and put in both the session scope and as hidden field of the form. During processing, check if the token is there and then remove it immediately from the session and continue processing. If the token is not there, then block processing. This will avoid the aforementioned kinds of problems.
Q4.What is POST-REDIRECT-GET Pattern?
The client gets a page with a form.The form POSTs to the server.The server performs the action, and then redirects to another page.The client follows the redirect.
For example, say we have this structure of the website as below
- /posts (shows a list of posts and a link to “add post”) and /
(view a particular post)
- /create (if requested with the GET method, returns a form posting to itself; if it’s a POST request, creates the post and redirects to the /
For retrieving posts /posts/
- Find the post with that ID in the database.
- Render a template with the content of that post.
For creating posts /posts/create might be implemented like this:
- If the request is a GET request for the Insert posts page Show an empty form with the target set to itself and the method set to POST.
- If the request is a POST request
- Validate the fields.
- If there are invalid fields, show the form again with errors indicated.
- Otherwise, if all fields are valid
- Add the post to the database.
- Redirect to /posts/
(where is returned from the call to the database)