I have SampleServlet class, within that I have override the doGet() method as follows
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String userid = (String)request.getServletContext().getInitParameter("userid");
out.print("Name = " + name + "<br>");
out.print("User id= " + userid+ "<br>");
}
Within my Web.xml i have added context parameters as follows,
<context-param>
<param-name>userid</param-name>
<param-value>ABC12345</param-value>
</context-param>
I used request.getServletContext().getInitParameter("userid"); statement to access that parameter.request.getServletContext().getInitParameter("userid"); Its work fine. However is there any difference between getServletContext().getInitParameter("userid"); and request.getServletContext().getInitParameter("userid"); Both give me same output but I don't have proper idea about these two.