An attribute is an object set into one of three other servlet API objects
1. ServletContext: All the servlet/JSP can set/get data. It is not thread safe.
2. HttpServletRequest (or ServletRequest):Servlet can set data and JSP view can get data
3. HttpSession: For each session, servlets/JSP have access. Session can persists across multiple requests from the same client. Session attribute is not thread safe. For example: Shopping cart.
1. ServletContext: All the servlet/JSP can set/get data. It is not thread safe.
2. HttpServletRequest (or ServletRequest):Servlet can set data and JSP view can get data
3. HttpSession: For each session, servlets/JSP have access. Session can persists across multiple requests from the same client. Session attribute is not thread safe. For example: Shopping cart.
As ServletContext is not thread safe, it is recommended to make call to get/set method under synchronized block.
To be noted that, all other servlet should synchronizes on the ServletContext.
To be noted that, all other servlet should synchronizes on the ServletContext.
synchronized(getServletContext())
{
getServletContext().setAttribute("adminEmail","admin@email.com");
out.println(getServletContext().getAttribute("adminEmail"));
}
As Session is not thread safe, it is recommended to make call to get/set method under synchronized block.
To be noted that, all other servlet should synchronizes on the Session.
To be noted that, all other servlet should synchronizes on the Session.
HttpSession session=request.getSession();
synchronized(session)
{
session.setAttribute("supportEmail","support@email.com");
out.println(session.getAttribute("supportEmail"));
}
No comments:
Post a Comment