?
Solved

context,config

Posted on 2006-05-02
4
Medium Priority
?
338 Views
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
0
Comment
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16584605
0
 
LVL 3

Accepted Solution

by:
arun_kuttz earned 500 total points
ID: 16584691
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..

<context-param>
   <param-name>var1</param-name>
   <param-value>test1</param-name>
</context-param>

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

config.getServletContext().getInitParamerter("var1");

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 :

<servlet>
...
   <init-param>
      <param-name>param1</param-name>
      <param-value>val1</param-value>
   </init-param>

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

config.getInitParam("param1");


-KuTtZ
</servlet>
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16584962
what is the need for config object there.

could i access that variable by context object only
0
 
LVL 3

Expert Comment

by:arun_kuttz
ID: 16585027
> 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.

-KuTtZ
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
These seven tips can help you create an extraordinary website, one that captivates audiences and has them wanting to return regularly for more. Keep reading to find out what your site is missing and what you need to add!
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

621 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