I am trying to check thread safety of a servletContext attribute. I know that ServletContext attributes are available in whole Web-App and can be accessed and modified by anyone, So I am trying to test the negative as scenario (i.e. context attribute was set by someone and updated by other) But it is not working.
Adding Code Snippet of actual code.
Servlet A
public class TestContextAttributeThreadSafety extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//synchronized(getServletContext()){
getServletContext().setAttribute("safety", "SetByThreadA");
//}
RequestDispatcher view = request.getRequestDispatcher("threadSafetyJsp.jsp");
view.forward(request, response);
}
Servlet B
public class TestContextAttributeThreadSafetyB extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//synchronized(getServletContext()){
getServletContext().setAttribute("safety", "SetByThreadB");
//}
RequestDispatcher view = request.getRequestDispatcher("threadSafetyJsp.jsp");
view.forward(request, response);
}
Index.html
<form name =" testThreadSafetyA" method ="GET" action="./TestContextAttr">
<br><br>
<h3> Test Context Attribute Thread Safety A</h3>
<input type="SUBMIT" value ="submit">
</form>
<form name =" testThreadSafetyB" method ="GET" action="./TestContextAttrB">
<br><br>
<h3> Test Context Attribute Thread Safety B</h3>
<input type="SUBMIT" value ="submit">
</form>
</body>
weB.XML
<servlet>
<servlet-name>/TestContextAttribute</servlet-name>
<servlet-class>com.incredible.controller.TestContextAttributeThreadSafety</servlet-class>
</servlet>
<servlet>
<servlet-name>TestContextAttributeB</servlet-name>
<servlet-class>com.incredible.controller.TestContextAttributeThreadSafetyB</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>/TestContextAttribute</servlet-name>
<url-pattern>/TestContextAttr</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestContextAttributeB</servlet-name>
<url-pattern>/TestContextAttrB</url-pattern>
</servlet-mapping>
threadSafetyJsp.jsp
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Thread Safety Test : </h1>
<%out.print(getServletContext().getAttribute("safety"));%>
</body>
</html>
Now I am running this app in Two Different Browsers. From One browser,I am clicking on first Submit button to get output SetByThreadA and from other browser, I am clicking 2nd submit button Which gives Output SetByThreadB.
Now, After clicking 2nd submit button, ServletContext attribute safety was updated by ThreadB. So, I go back to first browser where output of ThreadA is ( i.e. SetByThreadA )and click on page refresh button. Since It will again load jSP page, which is fetching value of ServletContext attribute safety, I am expecting the updated value of ServletContext on first Browser i.e. SetByThreadB. But, it is still showing SetByThreadA. So, I am not getting why thevalue is not getting updated. Please help
No you're not. You're not testing any such thing as thread safety here. You're just testing
setAttribute()
sequentially. There's no guarantee anywhere that these two pieces of code won't execute on the same thread. It's pointless. It works, and if it doesn't there is nothing you can do about it.Don't test the platform.