Difference between ServletConfig and ServletContext

Example code:

<servlet>
 <servlet-name>ServletConfigTest</servlet-name>
 <servlet-class>com.javapapers.ServletConfigTest</servlet-class>
 <init-param>
  <param-name>topic</param-name>
  <param-value>Difference between ServletConfig and ServletContext</param-value>
 </init-param>
</servlet>

The ServletContext object is contained within the ServletConfig object. That is, the ServletContext can be accessed using the ServletConfig object within a servlet. You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file.

Example code:

<context-param>
    <param-name>globalVariable</param-name>
    <param-value>javapapers.com</param-value>
</context-param>

This Servlet tutorial was added on 06/05/2008.

Comments on "Difference between ServletConfig and ServletContext" Tutorial:

  1. Praveen Kumar Jayaram says:

    Nice post. Gave an insight to Servlet config and context in short.

  2. Rajesh says:

    Thank you for given information on ServletContext and ServletConfig with examples, Am pretty much happy with this.

    But I want more information on ServletContext, and ServletConfig with more number of examples,

  3. SHOAIB says:

    why we need servlet mapping in web.xml file?

  4. SHOAIB says:

    why we need servlet mapping in web.xml file?

  5. Thadeuse says:

    what is the use of web.xml file why we are using?

  6. Vishal Chitransh says:

    Nice description.

  7. rohit says:

    Nice post. Simple to understand the difference

  8. Mahesh says:

    Your site is very good … very crisp and simple explanations ! Thanks !

  9. Rajesh says:

    Thank you for we given valuable information

  10. naveen says:

    nice post. simple and good.

  11. Abhishek Chaudhary says:

    Your posts are really very valuable.Description with examples .Nice way to write the post..

  12. Srinivas says:

    your blogs are in very simple and easy language..It’s very helpful for starters as well as who are preparing for interviews.

  13. Srinivas says:

    your blogs are in very simple and easy language..It’s very helpful for starters as well as who are preparing for interviews

  14. aryan says:

    very good description!

  15. Anonymous says:

    perfect guidance for unknown people

  16. prasad says:

    Nice & thanks.

  17. Amit says:

    Thanks 4 information..

  18. chukked says:

    crystal clear…

    Many Thanks

  19. rohit khaire says:

    thank sir,for giving information on this concept in so simple and lucid language with example.

  20. venkatrao says:

    Hi sir i want to more information about servletconfig and servletcontext

  21. Ankit says:

    Nice Answer
    Thanks

  22. anand says:

    nice one

  23. shaheed says:

    Its one the best and short difference i found, Great

  24. vairam says:

    It’s nice clear

  25. Dev says:

    More clarification required on ServletContext interface

  26. nonni says:

    nice one thanks

  27. vijayendra says:

    Nicely explained, cool.

  28. JavabynataraJ says:

    Hi Joseph, Good Explanation on ServletContext and SerlvetConfig.

  29. Rakhee says:

    Thanks a lot…well explained

  30. Anonymous says:

    Very Nicely Explained

  31. ramakrishna says:

    super more information me

  32. shanoor says:

    thank q for this post ..but i need some more examples for this please kindly post me ..my id shanoor1988@gmail.com

  33. Mahes says:

    This is nice and simple example. Thanks

  34. Sabyasachi Patra says:

    this is nice.

  35. Sadikhasan says:

    How Servlet compilation time is faster JSP? In Servlet each and every modification we have to explicitly compile it and not in jsp than how servlet is faster?

  36. Raja says:

    your all topics are very nice…
    thanks mr (Java King )

  37. sivakumar k says:

    You explained very nice sir…….
    But i have one doubt Please send me answer to my mail.

  38. Anonymous says:

    Thank U for Nice description,……….

  39. tanuj says:

    thank you … very brief and precise description…

  40. YSReddy says:

    To sadikhasan
    Always jsp is faster than servlet except
    servlets are faster than jsp only in one scenario. ie
    If you are invoking the java server page for first time, it has to convert the code into servlet code, and this servlet will be executed for the .class file. this will take time.
    And from second time onwards this .class file will directly execute.
    YOU CAN FIND THE JAVA FILE IN THIS LOCATION
    E:\JSP\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\creatBean\org\apache\jsp

  41. vishnu says:

    Upto the point. no beating around the bushes.very nice indeed.

  42. MInakshi says:

    Niec one I like this very much…easy to undestand.

  43. pawan says:

    when object will created for context & config parameter

  44. thangaraj vm says:

    Nice sir.l learned lot from this blogs

  45. manoj verma says:

    your way is very good.

  46. Tuhin says:

    Elaborated nicely….but it would be more helpful for me if some java code was there…to know how init-param and context-param come into picture in servlet !!!

    By d way…Thanks JOE…your blogs are really helpful….some known things become more crystal clear whenever I read your posts…

  47. Dharmesh says:

    Nice1

  48. vinayak says:

    nice

  49. Swapnil Sanghai says:

    Everything you wroe is good. But I didn’t understood that the ServletContext is within ServletConfig. As ServletContext is for whole webapp and the ServletConfig is for a servlet. How it is?

  50. KOTI says:

    thankQ

  51. Sayani Banerjee says:

    Nice one …

  52. Anonymous says:

    Pretty good and to the point

    – Yahoo! engineer

  53. Anonymous says:

    what is param in ur program?

  54. Anonymous says:

    good one..

  55. srinivas says:

    this has cleared all my doubts ..regarding servlet context and config ..

  56. BINOY says:

    can we configure multiple param-name with same name? if yes/no then why?

  57. subha says:

    The explanation is crystal clear

  58. Girish Varde says:

    For Servlet 3.0, ServletContext object is accessible across all JVMs in a distributed application, so please update.

  59. prafula says:

    good one …

  60. Thiru says:

    Where are we using ServeltContext in web applications?Live example is best to understand it’s usage.

  61. John Brtto M says:

    I just now completely understood about the servlet context and servlet container.

    Just a doubt?
    if web application is distributed over multiple JVM.how could be using of Servlet context?.And also if Web application is distributed multiple JVM.
    There is only one ServletConfig.Servlet Context is inside the ServeltConfig object.
    So how could you say that ServletContext Object cannot be used in the Distributed JVM
    Could you please give me Explanation.

    Thanks
    John Britto M

    thanks,

  62. Anonymous says:

    thank u for clarifying my doubts.

  63. Anonymous says:

    Thank u for clarifying my doubts about Servlet Config and ServletContext.

    Deepak Samantaray.
    bbsr,odisha

  64. paramjeet says:

    Thanks u very much

  65. Sunil Patil says:

    Thank u very much for this information.

  66. Sandeep says:

    Thanks u very much,

  67. avinash says:

    thank u for this great information…

  68. Anonymous says:

    not good.
    please exaplain well.
    cn u explain ServletContext context = getServletConfig().getServletContext();

  69. Babu says:

    Joe,
    Thanks for the info.

    Can u pls show an example of accessing ServletContext from ServletConfig within a Servlet

    TIA

  70. Ram says:

    Joe,
    Nice description

  71. Anonymous says:

    very good information thank u

  72. surya says:

    server side validation in servlets using SQL DATABASE

  73. Anonymous says:

    Your Blog post are too shallow, don’t give a clear picture for anything.

  74. Akanksha says:

    thank u it was a great help

  75. Anonymous says:

    Its nice.. But can u plz elaborate more with code

  76. sukh says:

    Great Explanation. Making concept very clear.
    Thanks for this kind information.

  77. Shah says:

    Please give it more example on servlet context and servlet config

  78. Pankaj says:

    Hi Sir! I am a new born baby in IT field and I want to stablish my career in JAVA Programming so pls send me more site like this in my mail id ‘pankajsen.mca@gmail.com’.
    Thank u.

  79. Prafulla says:

    Tanks sir

  80. Nagarajan says:

    hi sir,

    At last i found a simple site for java explanations.Thanks a lot.

  81. Mituranjan says:

    thanks , can you send me the running procedure of servletcontext & servletconfif with programs using weblogic server.

  82. Anonymous says:

    I very well understand the difference between servletconfig & servletcontext after read your blog.
    thank u joe……………..

  83. Anonymous says:

    Good Info … thanx

  84. steve says:

    I don’t understand much about the ServletContext and the ServletConfig. In my book, the ServletContext has a method called getRealPath();
    but when I tried it on the ServletConfig, it did not have such a method. Why not.

  85. Ashish says:

    nice example..

  86. jagdish says:

    thank sir,for giving information on this concept in so simple language with example.

  87. raju says:

    can we share data in applications using servletcontext explain clearly please………………….
    where we can share
    multiple servlets in ingle application. or multiple applications in entire servlet container

  88. sunil says:

    nice understandable theory
    please add some more code for servlet context and servlet config .

  89. kalai says:

    simple and superb……

  90. subbu says:

    i want more explanation on this

  91. ajith says:

    i understood.thanks

  92. anusha says:

    hi sir,

    At last i found a simple site for java explanations.Thanks a lot.I need more information about context and config.Send it to my mail id sir.
    can u sent the basic java concepts to me sir.

  93. Anonymous says:

    Interesting and good explanation.

    Thanks
    Hari N And Venu T

  94. Aditya Pandey says:

    Short and Simple explanation!!!!Nice one

  95. Srikanth says:

    its really helpful we can get an initial idea about servletcontext and servletconfg

    –> i would like to know the little more info on those topics
    -> when we use servletcontext (give me a sample example in real world entity)plzzzz
    -> why only servletcontext (there is no any other interfaces to use) plzzz

    thanks in advance

  96. praveen says:

    I need the difference between serverconfig and servletcontext with diagrams and simple examples.

  97. mohini says:

    nice explanation but i m not getting actual use of it can you explain with some example & give the code plzzzz and some more information about servlet life cycle thankssss

  98. Saurabh says:

    Servlet mapping tells that the request for the servlet will map on which url.
    and generated response will come with the mapped url in the web.xml.

  99. abhinav says:

    how servlet config is like a constructor in java class?

  100. Pankaj Sinha says:

    Really its always good to read your post , once again the way you have explained this topic is very nice. Thanks a lot.

  101. parteek says:

    very good description!

  102. parteek says:

    very good info

  103. Venkat says:

    After getting the servlet,to display the url in the browser.

  104. ashish says:

    How to differintiate in interview “send redirect ” and forward.

  105. Srinivas says:

    Thank you for given information on ServletContext and ServletConfig with examples, Am pretty much happy with this.

    But I want more information on ServletContext, and ServletConfig with more number of examples

  106. leela says:

    why means when the user make a request to particular web page servlet container(any server)need to identify the that particular servlet exactly so thats why servlet-mapping is required in web.xml.

  107. Anonymous says:

    I just now completely understood about the servlet context and servlet container.

    Just a doubt?
    if web application is distributed over multiple JVM.how could be using of Servlet context?.And also if Web application is distributed multiple JVM.
    There is only one ServletConfig.Servlet Context is inside the ServeltConfig object.
    So how could you say that ServletContext Object cannot be used in the Distributed JVM
    Could you please give me Explanation.

    Thanks
    John Britto M

    thanks,

  108. sriram says:

    this acts as an registry to register details about all the servlets used in project ..this is very imp

  109. Naveen says:

    Nice example in short form

  110. Naveen Kocherla says:

    Very good answer

  111. Anonymous says:

    Can i use ServletContext to pass values to all sessions in a servlet

  112. rajesh says:

    please send me any document about servlet config..

  113. Sangeeth Chitravel says:

    Servlet context is a shared memory for all the servlets in web application.

    Servlet config is seperate for each servlet in web application.

  114. prem says:

    simply good

  115. Anonymous says:

    your site ui was excellent idea was good,

  116. Anonymous says:

    Nice post ..

  117. saikrishna says:

    hi
    glad to see all these sort of useful information from you which helped me.
    and yeah please i would like to get all the IMPORTANT TOPICS right from the basics to my E-MAIL and then i will be very thankful to you.

    thanks in advance !!
    regards
    dubbasaikrishna@gmail.com

  118. rakesh says:

    such a good site for the concepts of java j2ee.

  119. Anonymous says:

    Because we most and should ensure url-pattern. That’s why we

  120. sumitkaushik says:

    why we use servletconfig and servlecontext

  121. Anonymous says:

    really helpful post..

  122. Anonymous says:

    write something extra apart from docs…thanks in adv…

  123. Anonymous says:

    Thank you for given information on ServletContext and ServletConfig with examples, Am pretty much happy with this.

    But I want more information on ServletContext, and ServletConfig with more number of examples,

    can you send me more information to me.
    this is my mail id: sathishummadi@gmail.com

  124. jayasree says:

    Thanks for given information on ServletConfig and ServletContext with examples

  125. anandhasarama says:

    config and context different in servlet

  126. sarfraj says:

    number of servlet context object present for an application?
    1)2 2)1 3)not fixed 4)each for a servlet

  127. Anonymous says:

    very clear

  128. Sheroo Pratap says:

    hi sir , i m your big fan

  129. Nethaji says:

    There will be always one and only ServletContext for web application.

    Remember One to Many concept
    Here One refers to ServletContext and Many refers to ServletConfig

  130. naresh says:

    sir i need more information about on this topic with example

  131. Anonymous says:

    thanks

  132. Pawan says:

    Hey Joe.
    Servlet context and ServletConfig are the interfaces.then how web container create objects for that?

  133. Lokesh Chandramohan says:

    can you please send me the More examples regardinth this

  134. vara prasad says:

    really i’m very happy now,bcoz a little ambiguty in these concepts.now i’m clarified……thanq sir…….

  135. vignesh pal pandian says:

    now i got cleared in servletconfig and servletcontext

  136. nagesh says:

    superb explanation…….these concepts now clear to me

  137. palanikumar says:

    sorry sir I cannot understood your way of explanation. please give real time example. when we are going to use. please explain pictorial way

Comments are closed for this "Difference between ServletConfig and ServletContext" tutorial.