Saturday, July 19, 2008

Java Advanced Concepts Part 2 - Servlet Filters

1. What are filters? How are they useful in web-applications?

Filters are useful to restrict the user request and perform some processing based on the URL. This acts like a firewall between the user request and the application.

For example, there would be a filter which is defined to be invoked when the URL starts with admin (adminLogin.do, adminSalesReport.do, adminCreateUser.do etc.,). This indicates that the filter and it would get processed only when the URL starts with admin to check if the given user is admin user or not) when the URL starts with any other word, say, login.do, it would ignore the admin authorization. After writing Filter class, you need to configure the same in WEB.XML.

The typical Filter call looks like the code given below:

public final class AdminOptions implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, UserAccessError, ServletException
{ String userName = request.getParameter(“userName”);
String password = request.getParameter(“password”);

Vector accessRights;
accessRights = LoginService.getAccess(userName,password);
if (accessRights==null) throw new UserAccessError();
chain.doFilter(request,response);
}

How to activate and configure the above Filter into WEB.XML

<filter>
<filter-name>AdminOptions</filter-name>
<filter-class>com.siva.AdminOptions</filter-class>
</filter>

<filter-mapping>
<filter-name>AdminOptions</filter-name>
<url-pattern>/admin*.jsp</url-pattern>
</filter-mapping>

The above Filter would be called when invoking any JSP that starts with “admin”. Also Also remember that you can have many levels of Filters that are executed as a chain one after another as required.

2. What is validation framework? Can we use it in Struts? How?

Validation framework is the framework that contains the necessary components for validation purposes. The validation routine can be used just by specifying the necessary validation and you need not repeatedly write the code for any such validation.

The validation framework can be easily included by downloading the jar file and placing it into LIB folder. Also required are the two important XML files that configures the required validation in the application. They are validation.xml and validator-rules.xml. The validator-rules.xml file contains all the validations and their mapping. But validation.xml is used to define the validations that are required in our applications. You need to place the following configuration settings in struts-config.xml

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>


3. List the configuration files used in Struts and their uses.

ApplicationResources.properties – This file defines the key – value pairs of the properties that are used in the application.
struts-config.xml – This xml file defines all the components that are used in struts application and how these components are interpreted. For example, when you define an action in JSP, struts-config file indicates the mapping of this action to the particular action class. It specifies whether the validation needs to be included or not, etc.,
These two files act like a bridge between controller layer and model layer.

4. What are the advantages of struts tag libraries? Why?

Struts provides plenty of tag libraries each is used for unique purpose. For example, bean tag library is used to create instance and set and retrieve the properties. logic tag library is useful to compare the bean for specific criteria.

These tag libraries are a bit faster when compared to scriptlets. Because the tag libraries are compiled components and no need to re-compile when JSP is executed. But scriplets need compilation which makes the process little slow.

5. What are the important components in Struts?
Action – Defines the busines part of the application.
ActionForm – Includes getter & setter methods and server-side validation routine.
ActionServlet – Controller that resolves all the requests from the user into required action.
ActionMapping – Defines which action class leads to which JSP.
Action Forward – Defines the destination JSPs.

1 comment:

Amd said...

Thank you...really helpful