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
Solved

servletContext vs servletConfig

Posted on 2014-03-27
8
666 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
tomcat not starting 6 68
spring jars download 1 35
Chrome and Firefox Java 5 50
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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