How can I declare global variable in Servlet and JSP which can beaccessed by all other Servlets / JSP present in the same container
I'm not a JSP/Servlet expert, but try cookies or static variables.
U can Use the ServletContext or HttpSession to store yourvariablesFor Example..HttpSession session;session = req.getSession(true);session.setAttribute("user_name",userName);Hope this info will help U .
Using session will only be "global" within each user.The ways that will make it truly global for each user,is the application object or any other number of waysinvolving the servlet configuration.
this is what the HttpSession is for. Use the setAttribute method. If youwant to share thevariable across several contexts make sure that you have enabled this inyour server settings.
Sorry. i mean to say in context-param tags in the web.xml file.
use a bean in application scopei.e. <jsp:useBean id="global" class="com.mybeans.Global" scope="application"/>then any variables inside that bean can be seen in any jsp.i.e. String str = global.getEmployeeName();
U can define ur parameter/ values in the web descriptor file, in theinit-param tags.
You can create a class with getter and setter for a variable which youwants to access globally and get the variable from any servlet.