I am today gonna discuss about Filters, static parameters which are init-param and context-param.
Filters:
Filters are the pluggable components where preprocessing and postprocessing is done. Preprocessing is executing the code before request is sent to servlet and postprocessing means executing the code after servlet code is finished and before the container sends the response back to client. Main tasks performed at preprocessing are decryption of message sent from client, data decompression, input validation etc. At postprocessing, tasks such as encryption of response message to be sent to client, data compression etc.
Example- I have created a login form validation(checks for empty fields) using filter. Filter will check for the input fields at it’s own end first. If any one of the field is empty, it will display a message and login form is shown again else request will be redirected to associated servlet.
index.html-
<!DOCTYPE html>
<html >
<head>
<title>Flat Login Form</title>
</head>
<body>
<h2>Login to your account</h2>
<form action="Login" method="post">
<input type="text" placeholder="Email" name="txtEmail"/>
<input type="password" placeholder="Password" name="txtPassword"/>
<button type="submit" value="Login">Login</button>
</form>
</body>
</html>
MyFilter.java-
doFilter is the method which has an important role in Filter. This is invoked every time by container when it has to apply filter to a resource. Container provides request and response object references to filter as argument. FilterChain is used to invoke the next filter in the chain.
@WebFilter("/MyFilter")
public class MyFilter implements Filter {
public MyFilter() {
}
public void init(FilterConfig fConfig) throws ServletException {
// initialize resources
}
public void destroy() {
// free the memory occupied by resources
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String email = request.getParameter("txtEmail");
String password = request.getParameter("txtPassword");
PrintWriter out = response.getWriter();
if(email.trim().equals("") || password.trim().equals("")) {
RequestDispatcher dispatcher = request.getRequestDispatcher("index.html");
dispatcher.include(request, response);
out.print("<center>Fill required field(s) and then proceed</center>");
}else {
chain.doFilter(request, response);
}
}
}
ServletClass.java-
Filter is applied to this class. When the empty fields are checked by filter, then request goes to this servlet class where we would write code for cross-checking the email and password(the fields in index.xml) from database which is done by JDBC.
@WebServlet("/Login")
public class ServletClass extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* here, write the code for vaidation of email and password using JDBC */
}
}
web.xml-
We need to configure web.xml. Declare the filter here and do the filter-mapping. Make sure the url-mapping pattern in filter mapping is same as url-mapping of servlet class to which we want to link our filter.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JavaEEProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>ServletClass</servlet-class>
</servlet>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/Login</url-pattern>
</filter-mapping>
</web-app>
Init-param:
This is static parameter stored in web.xml file. It has param-name(key) and param-value. This type of parameter is declared within the scope of a servlet of filter and has private scope to them only.
<servlet>
<display-name>ServletClass</display-name>
<servlet-name>ServletClass</servlet-name>
<init-param>
<param-name>Greetings</param-name>
<param-value>Hello</param-value>
</init-param>
</servlet>
Access the parameter value in servlet using ServletConfig object which can be used to get configuration information from web.xml file.
ServletConfig config=getServletConfig();
String greetings=config.getInitParameter("Greetings");
Context-param:
This is similar to init-param. The only difference is it’s global scope. It can’t be declared locally to any servlet or filter. Data is common to whole application. This is how global parameter is declared in web.xml.
<web-app>
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
</web-app>
Accessing the value in servlet.
ServletConfig config=getServletConfig();
String driver=config.getInitParameter("driver");