Summary for the difference between ServletConfig Parameter and ServletContext Parameter

when we need some global defined parameters in servlet, a good choice is to use ServletConfig init parameter or ServletContext init parameter. But to know which one is the better choice, we need to know the detailed differences between these two parameters:

ServletConfig

how to define:

In the DD file:

//In between <servlet> tag pair

<init-param>

  <param-name> paramName </param-name>

  <param-value> paramValue </param-value>

</init-param> 

In the servlet code 

getSerlvetConfig().getInitParameter("paramName");

note:

* Servlet Init Parameter Can't be used until the Servlet is initialized!!

* Config parameter is read only once!!

* Init Parameter is only for Servlet!! Each Servlet has it's own parameter

ServletContext

how to define:

In the DD file:

//In between <web-app> tag pair

<context-param>

  <param-name> paramName </param-name>

  <param-value> paramValue </param-value>

</context-param> 

In the servlet code 

getSerlvetContext().getInitParameter("paramName");

note:

* Context init parameter is for whole web app!!

* Context init only need to be defined in one DD and can be used anywhere in web app!!

* In a distributed environment, one Servletcontext per JVM

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Oracle Blogs Admin-Oracle

Search

Top Tags
Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today