Posted on 2006-05-02
Last Modified: 2010-04-01
Difference between context and config object in servlet.
suppose i want to share 1 variable into 5 servlets.

how could i have done through coding.

could you explain need for config object through coding
Question by:stmani2005
    LVL 23

    Expert Comment

    LVL 3

    Accepted Solution

    A servlet context is an object shared by all applications deployed in that container.. for ur requirement, u can use the servletContext object to gain access to variable to be shared across differect servlets (in ur case 5)..

    u must have the following snippet in ur web.xml..


    this can be accessed by using ServletContext object (which is obtained from the ServletConfig object) like so..


    As for the ServletConfig, it is specific to a particular Servlet, and u gain accsss to any initialPrarmeter variable u specify in the <servlet> </servlet> tag of the web.xml like so :


    and u access it using the config object, thats passed to the service method of the servlet.. like so


    LVL 8

    Author Comment

    what is the need for config object there.

    could i access that variable by context object only
    LVL 3

    Expert Comment

    > what is the need for config object there.

    u need the config object to get acces to the param1 variable..

    > could i access that variable by context object only

    u cannot access param1 using context object.. u need the config object (since it is specific to THAT servlet)
    while the var1 variable can be accessed by all servlets/JSPs using the context object (in servlets, u get the ServletContext object using the config object.. like iv showen u in the above code... in JSPs, u can access the Context object by using the implicit "application" object.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now