Updated April 6, 2023
Introduction to JSP Filters
In jsp have a lot of features to customize the web pages using jstl tags, customized tags, and elements. In normal filters is one of the features for iterating the datas or sort the datas based on the user requirements mainly it reduces the time complexity of the programs and also background task takes less time complexity of the process. Based on that jsp having own filters used in the java classes can be used for intercepting the client requests and it manipulates the server responses simultaneously it will authenticate the user credentials security purpose like encrypt the datas logging and auditing the datas.
Syntax
We can use the filters in jsp as web.xml. The following codes describe the syntax of the filters in xml.
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-pattern>
</filter-mapping>
The above codes are the basic syntax of the filters in web.xml configuration files we can also use that configuration in jsp page filters.
How does JSP Filter work?
The jsp filters are nothing but the java classes it can be used for intercept the client requests or either manipulating the response for each client requests from the servers. Then the jsp filters also used for authenticating the users for every request and also the authenticate datas to be more secured purposes like Encrypt the datas and used for store the user details with the help of Logging concepts and also Auditing the datas. Using javax.servlet class, we can use for creating the filters we can also implement the Filter interface.
The filters are not only in the jsp it also used for resources like java servlets and other static pages like HTML or some presentation techniques like images, videos, etc. In some kind of situation by using filters we can add the unnecessary complexity and the performance-wise its degraded because its very fast and accessible friendly for the user environments. With the help of filters used for filtering the java applications by using the filtering functionalities.
We can also different types of filters like:
- Authentication filters,
- Data Compression filters,
- Encryption filters,
- MIME chain filters,
- Logging filters and
- Tokenizing filters.
We all know about the filters are defined in the web.xml file and it will map into the servlet or jsp. Whenever we run the jsp codes it starts the jsp containers also automatically creates the instance of each filter it will be deployed in the deployment descriptor file.
We can use the multiple filters in the jsp using Authentication filters we can validate the user details with the help of data compression filters we can compress the user datas it will show it in the web page. Encryption filters used for encrypting the user datas. In every jsp or some other resources, files will be performed the action events it also triggers the user actions by using filter technique it is known as Filters that trigger resource access events.
In web pages we can use the images for highlighting the web pages more user friendly and attractively it will applicable in the filters also it stores the user datas in the Logging and Auditing filters and MIME Type chain filters used in the types used in the jsp filters in web.xml each user datas stores and retrives using tokenized filters format. The XSL/T filters used to transform the datas in the xl contents.
We can avoid the sensitive codes in the business logic like servlets we can write it in the filters then the user will call the servlets only not filters the same as jsp filters we can write it in the web.xml and the jsp page is associated with the filters. We can use the filters in the string replacements, filtering for storing the IP address of every user logins. We can concatenate the datas and strings using filters and also validate the registration form datas the image login authentication we can write the logic in the servlet codes it’s not secured so the hacker will hack the codes and also possible for the sql injection attacks if the database is available.
Examples of JSP Filters
Here are the following examples mention below
Example #1
Code:
<body>
<%
String n = request.getParameter("n1");
%>
<strong> Name of the user is: </strong> <%= n %>
</body>
public class tagHandler implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter started...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filters) throws IOException, ServletException {
String n1 = request.getParameter("n");
if(n1==null || n1.equals(""))
{
PrintWriter writer = response.getWriter();
String s="Name must be entered";
writer.println(s);
return;
}
filters.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("Filters deleted..");
}
}
<filter>
<filter-name>tagHandler</filter-name>
<filter-class>com.first.tagHandler</filter-class>
</filter>
<filter-mapping>
<filter-name>tagHandler</filter-name>
<url-pattern>/custom.jsp</url-pattern>
</filter-mapping>
Sample Output:
Example #2
Code:
public class tagHandler implements Filter {
@Override
public void init(FilterConfig filters) throws ServletException
{
System.out.println("Filter 2 started...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain f) throws IOException, ServletException
{
System.out.println("Filter 2 executing Before JSP codes");
f.doFilter(request, response);
System.out.println("Filter 2 executing after JSP codes");
}
@Override
public void destroy()
{
System.out.println("Filter 2 deleted..");
}
}
<body>
<strong>
filters Demo
</strong>
public class filterss implements Filter
{
@Override
public void init(FilterConfig filters) throws ServletException
{
System.out.println("Filter 1 started...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain f) throws IOException, ServletException
{
System.out.println("Filter 1 executing Before JSP codes ...");
f.doFilter(request, response);
System.out.println("Filter 1 executing after JSP codes...");
}
@Override
public void destroy() {
System.out.println("Filter 1 deleted..");
}
}
Sample Output:
Example #3
Code:
public class tagHandler implements Filter {
@Override
public void init(FilterConfig filters) throws ServletException {
System.out.println("Filter started...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain f) throws IOException, ServletException {
String user = request.getParameter("user");
if(user==null || user.equals(""))
{
PrintWriter writer = response.getWriter();
String m="Welcome to my domain";
writer.println(m);
return;
}
f.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("Filter deleted..");
}
}
<body>
<%
String user = request.getParameter("user");
%>
<%out.print(user); %>
</body>
Sample Output:
Conclusion
The filters is one of the important concepts of jsp and servlets because it regulates the data flow regarding the client requests based on their needs it applicable for servlets mainly filters are used for the web apps works implements of filters are very easy in the web applications
Recommended Articles
This is a guide to JSP Filters. Here we discuss an introduction to JSP Filter along with the working and examples for better understanding. You may also look at the following articles to learn more –