Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

servletContext vs servletConfig

Posted on 2014-03-27
8
Medium Priority
?
715 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 36

Accepted Solution

by:
mccarl earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 36

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 36

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 36

Expert Comment

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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

604 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