Solved

servletContext vs servletConfig

Posted on 2014-03-27
8
640 Views
Last Modified: 2014-04-13
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
0
Comment
Question by:gudii9
  • 4
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39960761
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
 
LVL 7

Author Comment

by:gudii9
ID: 39965254
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
 
LVL 7

Author Comment

by:gudii9
ID: 39965305
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:mccarl
ID: 39965530
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
 
LVL 35

Expert Comment

by:mccarl
ID: 39965533
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
 
LVL 7

Author Comment

by:gudii9
ID: 39997352
They should NOT have mentioned getInitParameterNames() in that context. Initialization parameters have nothing to do with attributes.

That makes more sense
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39998103
Glad that I could help! :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 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

16 Experts available now in Live!

Get 1:1 Help Now