{"id":78335,"date":"2025-07-27T00:29:43","date_gmt":"2025-07-27T00:29:43","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78335"},"modified":"2025-07-27T00:29:43","modified_gmt":"2025-07-27T00:29:43","slug":"java-servletconfig-vs-servletcontext","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78335","title":{"rendered":"Java: ServletConfig vs ServletContext"},"content":{"rendered":"\n<p>Here\u2019s a clear and copyright-free comparison of <strong><code>ServletConfig<\/code> vs <code>ServletContext<\/code><\/strong> \u2014 perfect for learning or blogging.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>ServletConfig vs ServletContext in Java<\/strong><\/h2>\n\n\n\n<p>Both <code>ServletConfig<\/code> and <code>ServletContext<\/code> are part of the <strong>Java Servlet API<\/strong> and provide access to configuration data, but they serve <strong>different scopes and purposes<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccb <strong>Comparison Table<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th><code>ServletConfig<\/code><\/th><th><code>ServletContext<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>Scope<\/strong><\/td><td>Per <strong>servlet<\/strong><\/td><td>Per <strong>web application<\/strong><\/td><\/tr><tr><td><strong>Purpose<\/strong><\/td><td>Pass <strong>init parameters<\/strong> to a specific servlet<\/td><td>Share data and resources <strong>across all servlets<\/strong><\/td><\/tr><tr><td><strong>Defined In<\/strong><\/td><td>Inside <code>&lt;servlet&gt;<\/code> tag in <code>web.xml<\/code><\/td><td>Inside <code>&lt;context-param&gt;<\/code> tag in <code>web.xml<\/code><\/td><\/tr><tr><td><strong>Accessed By<\/strong><\/td><td><code>getServletConfig()<\/code><\/td><td><code>getServletContext()<\/code><\/td><\/tr><tr><td><strong>Common Use<\/strong><\/td><td>DB name for one servlet, file path, etc.<\/td><td>Logging config, app version, global file paths, etc.<\/td><\/tr><tr><td><strong>Lifecycle<\/strong><\/td><td>Exists as long as the servlet is loaded<\/td><td>Exists as long as the web app is running<\/td><\/tr><tr><td><strong>Sharing<\/strong><\/td><td>Not shared between servlets<\/td><td>Shared by all servlets in the app<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddea Example Usage<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 <code>web.xml<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- ServletConfig (specific to one servlet) --&gt;\n&lt;servlet&gt;\n    &lt;servlet-name&gt;MyServlet&lt;\/servlet-name&gt;\n    &lt;servlet-class&gt;com.example.MyServlet&lt;\/servlet-class&gt;\n    &lt;init-param&gt;\n        &lt;param-name&gt;dbName&lt;\/param-name&gt;\n        &lt;param-value&gt;students&lt;\/param-value&gt;\n    &lt;\/init-param&gt;\n&lt;\/servlet&gt;\n\n&lt;!-- ServletContext (shared across servlets) --&gt;\n&lt;context-param&gt;\n    &lt;param-name&gt;appVersion&lt;\/param-name&gt;\n    &lt;param-value&gt;1.2.3&lt;\/param-value&gt;\n&lt;\/context-param&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\ud83d\udd39 In Servlet Code<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyServlet extends HttpServlet {\n\n    public void doGet(HttpServletRequest request, HttpServletResponse response)\n            throws ServletException, IOException {\n        \n        \/\/ ServletConfig example\n        ServletConfig config = getServletConfig();\n        String dbName = config.getInitParameter(\"dbName\");\n\n        \/\/ ServletContext example\n        ServletContext context = getServletContext();\n        String appVersion = context.getInitParameter(\"appVersion\");\n\n        response.getWriter().println(\"DB: \" + dbName + \", App Version: \" + appVersion);\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>ServletConfig<\/code> for <strong>servlet-specific setup<\/strong>.<\/li>\n\n\n\n<li>Use <code>ServletContext<\/code> for <strong>application-wide configuration and shared data<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Let me know if you want:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udcdd A Markdown or HTML version<\/li>\n\n\n\n<li>\ud83d\udcc4 A downloadable PDF version<\/li>\n\n\n\n<li>\ud83c\udfaf Real-world examples (like uploading files, database configs) using context\/config.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Ref: OpenAI\/ChatGPT<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a clear and copyright-free comparison of ServletConfig vs ServletContext \u2014 perfect for learning or blogging. \ud83d\udd04 ServletConfig vs ServletContext in Java Both ServletConfig and ServletContext are part of the Java Servlet API and provide access to configuration data, but they serve different scopes and purposes. \ud83d\udccb Comparison Table Feature ServletConfig ServletContext Scope Per servlet &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78335\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1975],"tags":[],"class_list":["post-78335","post","type-post","status-publish","format-standard","hentry","category-anything-java","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":74548,"url":"http:\/\/bangla.sitestree.com\/?p=74548","url_meta":{"origin":78335,"position":0},"title":"Web Component and Web Application Development #servlet #java #JSP","author":"Sayed","date":"May 10, 2022","format":false,"excerpt":"Trial Videos: Introduce Servlet Concepts Trial: Intro to Topics in Servlet-based Web Application Development. \u09b8\u09be\u09b0\u09ad\u09cd\u09b2\u09c7\u099f \u098f\u09b0 \u099f\u09aa\u09bf\u0995 \u0997\u09c1\u09b2\u09cb - \u099c\u09beURL Trial: Servlet InterfaceURL Trial: Generic ServletURL Trial: HttpServlet: Java Based Web Application DevelopmentURL Trial Video: Life Cycle of Servlet (In Bengali)URL Trial Video: How to Create Servlets (In Bengali)URL Trial:\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":74780,"url":"http:\/\/bangla.sitestree.com\/?p=74780","url_meta":{"origin":78335,"position":1},"title":"Java: Web Component and Web Application Development","author":"Sayed","date":"May 31, 2022","format":false,"excerpt":"(In the Bengali Language) Learn the Concepts (no code demo yet) Java (Java EE, J2EE) Web Component and Web Application Development. https:\/\/lnkd.in\/eRqd4bjX SaLearningSchool-ShopForSoul-SitesTree: For Paid Training: https:\/\/lnkd.in\/dNrqGDuP 5:57 Trial: HttpServlet: Java Based Web Application Development https:\/\/lnkd.in\/eTReji7H 9:33 Trial: Generic Servlet https:\/\/lnkd.in\/dd7z7fJZ 10:34 Trial: Servlet Interface https:\/\/lnkd.in\/eDh8XuSy 9:50 Trial Video: Life\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":74781,"url":"http:\/\/bangla.sitestree.com\/?p=74781","url_meta":{"origin":78335,"position":2},"title":"Playlist: Anything Java","author":"Sayed","date":"May 31, 2022","format":false,"excerpt":"Playlist: Anything Java: https:\/\/www.youtube.com\/playlist... #SaLearningSchoolShopForSoulSitesTree, #SaLearningSchool, #ShopForSoul, #SitesTree Starting Java training basic file operations in java basic java language concepts bengali javascript debugging data conversion in jsf applications debugging javascript code Docker and Vagrant Optional Java and\/or PHP Platform ejb application with bea weblogic eclipse experiment eclipse ant how to\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69282,"url":"http:\/\/bangla.sitestree.com\/?p=69282","url_meta":{"origin":78335,"position":3},"title":"JSP: Random Information #37","author":"Author-Check- Article-or-Video","date":"August 16, 2021","format":false,"excerpt":"Three JSP constructs: scripting elements, actions, directives Scripting elements: expressions, scriptlets, declarations Expression = translates to println in servlets in _jspService methods < % = expression %> < % = new java.util.Date() %> inserts values directly to the output scriptlet: block of java code : directly inserted into the related\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":66330,"url":"http:\/\/bangla.sitestree.com\/?p=66330","url_meta":{"origin":78335,"position":4},"title":"Servlet Random Information #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 18, 2021","format":false,"excerpt":"Web component information sharing: web context (ServletContext), request, session, and page ServletContext - Scope: entire web-application (Servlets & JSPs) page\/PageContext : A single point of access for many of the attributes of a JSP page For each access of the servlet a thread is created. In applications a thread can\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10160,"url":"http:\/\/bangla.sitestree.com\/?p=10160","url_meta":{"origin":78335,"position":5},"title":"ShowMessage.java Servlet that demonstrates the use of initialization parameters.","author":"","date":"August 16, 2015","format":false,"excerpt":"ShowMessage.java\u00a0 Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things: \u00a0\u00a0\u00a0 * Put the modified web.xml file in the WEB-INF directory. \u00a0\u00a0\u00a0 * Restart the server. \u00a0\u00a0\u00a0 * Use the registered servlet name (i.e., the URL http:\/\/host\/servlet\/ShowMsg), not the\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78335"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78335\/revisions"}],"predecessor-version":[{"id":78336,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78335\/revisions\/78336"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78335"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}