Solved

servletContext vs servletConfig

Posted on 2014-03-27
8
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 68
printf performancy 11 86
Eclipse for Java EE development 2 45
jmeter usage 4 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

733 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