Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of girionis
girionis
Flag of Greece image

Avatar of gudii9

ASKER

>>>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
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.
Avatar of gudii9

ASKER

>>>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
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

please  advice
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial