servlet config and servlet context

Hi,

I would like to know differences, uses, advantages, disadvantages of servlet config and servlet context.

In the servlet config even though we change values only in web.xml init-param name and value i still have to re run the whose servlet again which is restarting the server any way. Not compiling servlet seems to me not a big advantage.


when do we us servlet context. why do we ned to set and get attributes. I tried small example where i tried to set the attribute. In other servlet i tried to get the same attribute from servlet cotnext but getting null. How can i fix it

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Context1 extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		ServletContext sc = getServletContext();
		sc.setAttribute("city", "seoul ");
		

	}
}










import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Context2 extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		ServletContext sc = getServletContext();

		Enumeration en = sc.getInitParameterNames();

		while (en.hasMoreElements()) {
			Object obj = e1.nextElement();
			out.print("<br>" + obj + "  =======   "
					+ sc.getInitParameter(obj.toString()));

		}
		out.println("capital is " + sc.getAttribute("city"));
	}

}

Open in new window

Please advise.
what are the differences between parameters and attributes. when do we use which one.
Please advise.
Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
0
gudii9Author Commented:
>>>ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class



How servletcnfig is similar to constructor. What kind of initialization parameters we pass. Are intialization parameters similar to arguments we pass to constructor?



>>> servlet context is applicable only within a single Java Virtual Machine. If a web application is distributed between multiple JVM this will not work. For understanding, this is like a application global variable mechanism for a single web application deployed in only one JVM



How to share info between web application deployed on multiple JVMs if servlet contxt cannot serve in that scenario.

>>what are the differences between parameters and attributes. when do we use which one.

when to use parameter when to use attribute.
Please advise
0
girionisCommented:
How servletcnfig is similar to constructor. What kind of initialization parameters we pass. Are intialization parameters similar to arguments we pass to constructor?
In the constructor you pass initialisation parameters. The same can be done with the servlet config, since you don't have direct access to the servlet constructor (it is only used by the container).

How to share info between web application deployed on multiple JVMs if servlet contxt cannot serve in that scenario.
This is usually done by using a database, or some other means for sharing information (like a file).

when to use parameter when to use attribute.
When to use elements versus attributes.
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

gudii9Author Commented:
>>>when to use parameter when to use attribute.
When to use elements versus attributes.



Is there is material specific to java but not xml. Please advise
0
Amitkumar PSr. ConsultantCommented:
Parameter is what that decides a configuration to the application (ie. context param in case of servlet context) or servlet (ie. param in case of servlet). Value of parameter remains same and can not be changed during multiple invocation. That's why you will not see setParameter() method in the servlet api. Example of parameters like.. (env : DEV/QA/PROD, dbparams : DB Host, DB Port etc)

Attribute is what a data to the application and it can be passed from one servlet to another using request/response. Yes, it can be changed and have setter methods in servlet api.

Hope this clears. :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
makes more sense.

>>>Parameter is what that decides a configuration to the application (ie. context param in case of servlet context) or servlet (ie. param in case of servlet).


when to use context param and when to use servlet param while deciding configuration to the application.

Please advise what are advantages, disadvatages of using context param, servlet param, attributes with sample simple examples
0
gudii9Author Commented:
please  advice
0
girionisCommented:
Use context parameters when you want to deal with the servlet context, use servlet parameter when you want to deal with the servlet and the HTTP request.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.