Source Code : Context accessor
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Context accessor
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ContextAccessor extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
//get a servlet context attribute
ContextObject contextObj = (ContextObject) getServletContext()
.getAttribute("com.java2s.ContextObject");
if (contextObj != null)
contextObj.put(request.getRemoteAddr(), "" + new java.util.Date());
//display
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out
.println("<html><head><title>Servlet Context Attribute</title></head><body>");
if (contextObj != null) {
out.println("<h2>Servlet Context Attribute Values</h2>");
out.println(contextObj.getValues());
} else {
out.println("<h2>Servlet Context Attribute is Null</h2>");
}
out.println("</body></html>");
} //end doGet
}
//ContextObject.java
class ContextObject {
private Map map;
public ContextObject() {
map = Collections.synchronizedMap(new HashMap());
}
public void put(Object key, Object value) {
if (key == null || value == null)
throw new IllegalArgumentException(
"Invalid parameters passed to ContextObject.put");
map.put(key, value);
}
public Map getMap() {
return map;
}
public String getValues() {
StringBuffer buf = new StringBuffer("");
Set set = map.keySet();
synchronized (map) {
Iterator i = set.iterator();
while (i.hasNext())
buf.append((String) i.next() + "<br>");
}
return buf.toString();
}
public String toString() {
return getClass().getName() + "[ " + map + " ]";
}
}
Thank with us