我已經編寫了ServletContext
對象的演示程序,其中我使用context.setAttribute(arg1,arg2)
來設置值。我想在另一個servlet中訪問同一個對象。 如何在另一個servlet中訪問context對象設置的值。如何在另一個servlet中訪問相同的ServletContext對象?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = "Amrut";
ServletContext context = request.getServletContext();
context.setAttribute("contextuname", name);
out.println("Context==>" + context.getAttribute("contextuname"));
}
我的問題是,訪問這個對象我要創建ServletContext
對象,並使用context.getAttribute(arg1,arg2)
,我會得到價值。或者有另外的價值來做到這一點。
每個應用程序只有一個「ServletContext」可用。它的'ServletConfig'是不同的。 –