Difference between ServletConfig and ServletContext
Last modified on September 12th, 2014 by Joe.
- Signature: public interface ServletConfig
ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class.
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>
- Signature: public interface ServletContext
ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container, for example, to get the MIME type of a file, to get dispatch requests, or to write to a log file. That is to get detail about its execution environment. It is applicable only within a single Java Virtual Machine. If a web applicationa is distributed between multiple JVM this will not work. For understanding, this is like a application global variable mechanism for a single web application deployed in only one JVM.
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>
Nice post. Gave an insight to Servlet config and context in short.
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,
why we need servlet mapping in web.xml file?
why we need servlet mapping in web.xml file?
what is the use of web.xml file why we are using?
Nice description.
Nice post. Simple to understand the difference
Your site is very good … very crisp and simple explanations ! Thanks !
Thank you for we given valuable information
nice post. simple and good.
Your posts are really very valuable.Description with examples .Nice way to write the post..
your blogs are in very simple and easy language..It’s very helpful for starters as well as who are preparing for interviews.
your blogs are in very simple and easy language..It’s very helpful for starters as well as who are preparing for interviews
very good description!
perfect guidance for unknown people
Nice & thanks.
Thanks 4 information..
crystal clear…
Many Thanks
thank sir,for giving information on this concept in so simple and lucid language with example.
Hi sir i want to more information about servletconfig and servletcontext
Nice Answer
Thanks
nice one
Its one the best and short difference i found, Great
It’s nice clear
More clarification required on ServletContext interface
nice one thanks
Nicely explained, cool.
Hi Joseph, Good Explanation on ServletContext and SerlvetConfig.
Thanks a lot…well explained
Very Nicely Explained
super more information me
thank q for this post ..but i need some more examples for this please kindly post me ..my id shanoor1988@gmail.com
This is nice and simple example. Thanks
this is nice.
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?
your all topics are very nice…
thanks mr (Java King )
You explained very nice sir…….
But i have one doubt Please send me answer to my mail.
Thank U for Nice description,……….
thank you … very brief and precise description…
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
Upto the point. no beating around the bushes.very nice indeed.
Niec one I like this very much…easy to undestand.
when object will created for context & config parameter
Nice sir.l learned lot from this blogs
your way is very good.
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…
Nice1
nice
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?
thankQ
Nice one …
Pretty good and to the point
– Yahoo! engineer
what is param in ur program?
good one..
this has cleared all my doubts ..regarding servlet context and config ..
can we configure multiple param-name with same name? if yes/no then why?
The explanation is crystal clear
For Servlet 3.0, ServletContext object is accessible across all JVMs in a distributed application, so please update.
good one …
Where are we using ServeltContext in web applications?Live example is best to understand it’s usage.
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,
thank u for clarifying my doubts.
Thank u for clarifying my doubts about Servlet Config and ServletContext.
Deepak Samantaray.
bbsr,odisha
Thanks u very much
Thank u very much for this information.
Thanks u very much,
thank u for this great information…
not good.
please exaplain well.
cn u explain ServletContext context = getServletConfig().getServletContext();
Joe,
Thanks for the info.
Can u pls show an example of accessing ServletContext from ServletConfig within a Servlet
TIA
Joe,
Nice description
very good information thank u
server side validation in servlets using SQL DATABASE
Your Blog post are too shallow, don’t give a clear picture for anything.
thank u it was a great help
Its nice.. But can u plz elaborate more with code
Great Explanation. Making concept very clear.
Thanks for this kind information.
Please give it more example on servlet context and servlet config
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.
Tanks sir
hi sir,
At last i found a simple site for java explanations.Thanks a lot.
thanks , can you send me the running procedure of servletcontext & servletconfif with programs using weblogic server.
I very well understand the difference between servletconfig & servletcontext after read your blog.
thank u joe……………..
Good Info … thanx
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.
nice example..
thank sir,for giving information on this concept in so simple language with example.
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
nice understandable theory
please add some more code for servlet context and servlet config .
simple and superb……
i want more explanation on this
i understood.thanks
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.
Interesting and good explanation.
Thanks
Hari N And Venu T
Short and Simple explanation!!!!Nice one
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
I need the difference between serverconfig and servletcontext with diagrams and simple examples.
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
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.
how servlet config is like a constructor in java class?
Really its always good to read your post , once again the way you have explained this topic is very nice. Thanks a lot.
very good description!
very good info
After getting the servlet,to display the url in the browser.
How to differintiate in interview “send redirect ” and forward.
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
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.
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,
this acts as an registry to register details about all the servlets used in project ..this is very imp
Nice example in short form
Very good answer
Can i use ServletContext to pass values to all sessions in a servlet
please send me any document about servlet config..
Servlet context is a shared memory for all the servlets in web application.
Servlet config is seperate for each servlet in web application.
simply good
your site ui was excellent idea was good,
Nice post ..
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
such a good site for the concepts of java j2ee.
Because we most and should ensure url-pattern. That’s why we
why we use servletconfig and servlecontext
really helpful post..
write something extra apart from docs…thanks in adv…
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
Thanks for given information on ServletConfig and ServletContext with examples
config and context different in servlet
number of servlet context object present for an application?
1)2 2)1 3)not fixed 4)each for a servlet
very clear
hi sir , i m your big fan
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
sir i need more information about on this topic with example
thanks
Hey Joe.
Servlet context and ServletConfig are the interfaces.then how web container create objects for that?
can you please send me the More examples regardinth this
really i’m very happy now,bcoz a little ambiguty in these concepts.now i’m clarified……thanq sir…….
now i got cleared in servletconfig and servletcontext
superb explanation…….these concepts now clear to me
sorry sir I cannot understood your way of explanation. please give real time example. when we are going to use. please explain pictorial way