servletContext vs servletConfig

Hi,

I am reading about servletContext and servletConfig as below.
http://www.javatpoint.com/servletcontext
http://www.javatpoint.com/servletconfig

Both concepts seems more or less same to me. What are differences, advantages, disadvantages, practical uses of each.

The source code for sevletContext example not given but it was given for servletConfig example. I created dynamic web project for servletConfig which is running fine at
http://localhost:8080/ServletConfig/servlet1

I got output

Name: username value: system
Name: password value: oracle

My question is how i can copy paste servletConfig dynamic project again in eclipse so that i can use that as base reference to build servleContext project on top of that rather than starting from scratch. I went to file---copied servletConfig project---now i went to file again and i do not see paste option enabled. How to do paste?
public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.

How above method is present in both places?

please advise
Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
I am reading about servletContext and servletConfig...

Both concepts seems more or less same to me.
No, not at all. The following is simplifying things a bit but it should be ok... Remember that you have "web applications" and within a web application you can have a number of servlets. So the ServletContext is related to the entire web application whereas a ServletConfig is related to one particular servlet, and each servlet has their own distinct ServletConfig object (it gets passed to the servlet in the servlet's init() method). And you can access the "common" ServletContext object via the individual ServletConfig.getServletContext() method. All the servlet's ServletConfigs would return the same instance for the ServletContext.

public Enumeration getInitParameterNames()

How above method is present in both places?
While they have the same name, they are different methods that return slightly different things...

ServletContext.getInitParameterNames() will return the names of parameters that are defined by the <context-param> tags in the web.xml. These are defined outside of any specific <servlet> tag in the web.xml as they are common to the whole web app. Hence this is why you get them via the common ServletContext object.

ServletConfig.getInitParameterNames() will return the names of parameters that are defined by the <init-param> tags that are inside the individual <servlet> tags. Hence, only the servlet that defines the <init-param> will be able to access them via it's ServletConfig object. Other servlets work see those parameters (only their own parameters)



Side question....
how i can copy paste servletConfig dynamic project in Eclipse
I'm not sure what you mean by File->Copy, because in my version Eclipse there is no entry in the File menu for that. To copy a project, I can simply select the project from the Project Explorer on the left, go to "Edit" -> "Copy", and then immediately go to "Edit" -> "Paste" and it will bring up a dialog to give the new project a new name, and then I have a copy of the project.
0
 
gudii9Author Commented:
That makes more sense. In the next concept also(http://www.javatpoint.com/attribute) i see same method


public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.
How they are using it there as well?
0
 
gudii9Author Commented:
I can simply select the project from the Project Explorer on the left, go to "Edit" -> "Copy", and then immediately go to "Edit" -> "Paste" and it will bring up a dialog to give the new project a new name, and then I have a copy of the project.

Let us say i gave new project name as 'TestProject'. I do not to change any .classpath entry or anything else fro TestProject?. If  i decide to run on different server sy tomcat 6 for TestProject instead of tomcat 7 as in old project , do  i need to change any entries in the copied project. Please advise
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.

How they are using it there as well?
They should NOT have mentioned getInitParameterNames() in that context. Initialization parameters have nothing to do with attributes. And that is why you DON'T see any use of that method in the code samples below it.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
If  i decide to run on different server sy tomcat 6 for TestProject instead of tomcat 7 as in old project , do  i need to change any entries in the copied project
As long as the old project (hence therefore the new project aswell) is compatible with Tomcat 6 aswell, then no, you shouldn't need to change anything.
0
 
gudii9Author Commented:
They should NOT have mentioned getInitParameterNames() in that context. Initialization parameters have nothing to do with attributes.

That makes more sense
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Glad that I could help! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.