04 Aug
Posted by: admin in: Java, java snippets, java web apps
There are two options to store parameters in web.xml:
- context parameters – available to the entire scope of the web application
- init parameters – available in the context of a servlet or filter in the web application
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<description>This is a context parameter example</description>
<param-name>ContextParam</param-name>
<param-value>ContextParam value</param-value>
</context-param>
...
<web-app>
The following code can be be invoked from a servlet or a filter to retrieve the ContextParam value. The parameter can be read successfully from any servlet or filter class.
@Override
public void init(ServletConfig config) throws ServletException {
String contextParam = config.getServletContext().getInitParameter("ContextParam");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String contextParam = this.getServletContext().getInitParameter("ContextParam");
...
}
...
}
String value = this.getServletContext().getInitParameter("ContextParam");
...
<servlet>
<servlet-name>A Servlet</servlet-name>
<servlet-class>com.controller.TestServlet</servlet-class>
<init-param>
<description>This is an init parameter example</description>
<param-name>InitParam</param-name>
<param-value>init param value</param-value>
</init-param>
</servlet>
...
The following code can be be invoked to retrieve the value of InitParam. The parameter can be accessed only from com.controller.TestServlet.
public class DefaultController extends HttpServlet
{
@Override
public void init(ServletConfig config) throws ServletException {
String initParam = config.getInitParameter("InitParam");
}
//or
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String initParam = getServletConfig().getInitParameter("InitParam");
...
}
...
}
It’s possible to iterate through the paramters if required:
public class DefaultController extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Enumeration contextParams = getServletContext().getInitParameterNames();
Sytem.out.println("context-params: ");
while (params.hasMoreElements()) {
String name = (String) params.nextElement();
Sytem.out.println(name + " = " + config.getInitParameter(name));
}
Enumeration initParams = getServletConfig().getInitParameterNames();
Sytem.out.println("init-params: ");
while (params.hasMoreElements()) {
String name = (String) params.nextElement();
Sytem.out.println(name + " = " + config.getInitParameter(name));
}
...
}
...
}
4 Responses
benaz
29|Dec|2009 1hey good useful stuff….have u used jrun as your server to run scriptlets ? do u need 2 change da path? where is Jintegra.jar nd exel.jar files.
can u plz tell me how 2 run scriptlets in jrun from scratch?
javagenious
16|Feb|2010 2Thanks for the info. Can u tell if i can set an object as a servlet context param
Tandon
05|Mar|2010 3Hey!
Can the context-param or init-param tags contain multiple values for the same paramater?
param1
val1
val2
That is, is the above possible?
lavanya
28|Apr|2010 4Good , clear and useful one
tx
Leave a reply
Search
Books I Recommend
Categories
Blogroll
Recent Posts
Recent Comments
Tags