Wednesday, July 31, 2013
In current times, we are always trying to complete our tasks in least possible amount of time. This has given rise to the need for multithreading. In multithreaded application, we have more than one threads running at the same time.  We can see that when we are writing some document in Microsoft Word we can write as well as run the spell-checker at the same time. This is done by multithreading. But the real problem arises when a shared resource is being accessed by more than one thread at the same time.
     For an example, let us consider that we we are in a situation where a couple Williams and Jennifer, both have an access to the same bank account but have two ATM cards one for each. Now, both of them are trying to withdraw a certain amount from the same account. Now here arises the problem. Suppose, they have now currently $1000 in account and can withdraw a maximum of $300. Before withdrawing, they must check balance and then withdraw. Now Williams checks the balance and waits for withdrawing while in the meantime Jennifer checks balance and sees that she can also withdraw a maximum of $300. Since both have the information that they can withdraw a maximum of $300, it results in "inconsistency" of data. This should be avoided. So we need synchronization.
      Synchronization will help in atomic operation. If we take our previous example then Jennifer should not be allowed to access the account until and unless Williams has completed his operation. So, she should be locked from accessing it.
      Object locking in Java can be done in two way - intrinsic locks and explicit locks. Intrinsic lock is achieved using the "synchronized" keyword in Java. On using this in a method or block will ensure that all the operations done inside that method or block will be done in a single operation. Explicit locks is done using Lock objects.
      We will discuss in detail on intrinsic and explicit locks using Producer-Consumer example in our next posts. Keep in touch with us.
Wednesday, July 24, 2013
Today I will show you how you can use the power of filters to do session tracking. We will create a special type of filter here that will do session tracking. You people must have experienced that when you visit some websites(like facebook,twitter etc) you cannot access all the pages until you are logged in. Today we will implement this feature. We will write a session filter that checks for a certain attribute in the session object to see whether the user is already logged in or not. If the user is logged in then he can access those pages otherwise will be redirected to the home page. Here the filter will act as a phantom and intercept the request and according to that send the response.
   Here we will consider that as soon as the user logs in a attribute named "user" is set with object of some User class that contains all user details using methods getSession(false) and getAttribute(). So we will test whether taht attribute exists or not. If it exists then he will be able to access the requested page, otherwise will be redirected to login page using sendRedirect().
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class SessionFilter implements Filter {

public void destroy() {
System.out.println("SessionFilter destroyed");
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;   //casting
HttpServletResponse res=(HttpServletResponse)response;
HttpSession session=req.getSession(false);  //getting session object
if(session!=null){
   Object usr=session.getAttribute("user");  //getting attribute
   if(usr==null){  //if its null then not logged in
   System.out.println("Invalid Session");
   res.sendRedirect("./login.jsp");  //redirecting to login page
   }
   else
       chain.doFilter(req, res);  //permitting to access if logged in
}
else
    res.sendRedirect("./login.jsp");  //redirecting if no session object
}

public void init(FilterConfig fConfig) throws ServletException {
System.out.println("SessionFilter initialized");
}
}

NOTE : If you are using servlet 3.0 then mention the url's for which you want to add this filter using WebFilter annotation. But if you are using Servlet2.5 then use the <filter-mapping> tag in web.xml to map this filter to url's. So depending on your web-app use the different way you want , so I am not mentioning it in my code.
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

Total Pageviews

Followers


Labels

Popular Posts

free counters