DD elements <context-param> and <init-param> both can be retrieved by the getInitParameter() method, in the servlet code.
Now the question is, how does it differentiate <context-param> and <init-param>?
Servlet init parameters are for a single servlet only. Nothing outside that servlet can access that. It is declared inside the <servlet> tag of Deployment Descriptor, on the other hand context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag <context-param> directly inside the <web-app> tag.
The methods for accessing context init parameter is
getServletContext().getInitParameter("name");
whereas the method for accessing servlet init parameter is
getServletConfig().getInitParameter("name");
As explained by Adeel Ansari, here, it depends on what object are you invoking the method getInitParameter() in the servlet code.
All servlets extends from and hence are instance of GenericServlet.
.
DD elements <context-param> can be retrieved by:
ServletContext context = this.getServletContext();
String paramValue = context.getInitParamter("paramName");
.
DD elements <init-param> both can be retrieved by:
ServletConfig config = this.getServletConfig();
String paramValue = config.getInitParamter("paramName");
Also note that since GenericServlet class implements ServletConfig interface, your servlet class is also ServletConfig (implies this = this.getServletConfig() ). Hence you can also get DD elements <init-param> directly by:
String paramValue = this.getInitParamter("paramName");
.
You can try this by having same param-name in both DD elements with different values and then print it in your servlet.
Related
I'm working on a requirement where I need to write the servlet to the context path of the application, according to servlet specification document empty string i.e. "" mapping should work for application root context. I have tried different combinations like
<servlet>
<servlet-name>Redirect</servlet-name>
<servlet-class>RedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Redirect</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Redirect</servlet-name>
<servlet-class>RedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Redirect</servlet-name>
<url-pattern>""</url-pattern>
</servlet-mapping>
these are not working. Is there any other way to write the servlet for the context path
for path "/" there is a servlet already. Requirement is to write the servlet for context path
Empty string is for application root context.
It's a legacy / backwards compat feature.
The recommended declaration for root context is still /, and the returned string for application root context will always be "" (empty string).
That being said, this little bit of trivia about application root context isn't relevant to your issue at hand.
You are dealing with url-pattern, a different concept.
The url-pattern rules are applied after the request has matched an application root context.
A url-pattern that has quotes is invalid.
A url-pattern that is empty is pointless, as it will never match an incoming request and you'll never have a dispatched request sent to it.
I have a Servlet Filter and due to my business logic Filter uses some variables that are initializing in when servlet's method init() invoke. So the question is: is any possibility to init filter after servlet. My Web.xml is next:
...
<servlet>
<servlet-name>CommonsServlet</servlet-name>
<servlet-class>com.promptlink.dslib.gwt.common.server.rpc.CommonsServletImpl</servlet-class>
</servlet>
...
<filter>
<filter-name>CommonServletFilter</filter-name>
<filter-class>com.promptlink.dslib.gwt.common.server.httpListeners.CommonServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CommonServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<servlet-name>CommonsServletImpl</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
...
Maybe is any way to configure filter not in xml? I've heart that it is possible to add filter to ServletContext, but i need to add a mapping to filter too
Filters are initialized before servlets, see here for the details.
But you can create a ServletContextListener which is loaded at application startup before any filter or servlet, initialize your variables in the listener, and let your serlvets and filters then use the already initialized variables.
The listener can also add your servlets and filters programmatically, see ServletContext.addFilter() and ServletContext.addServlet().
I know that I can set init params for a jsp page using web.xml. For example
<servlet>
<servlet-name>init</servlet-name>
<jsp-file>/init.jsp</jsp-file>
<init-param>
<param-name>test</param-name>
<param-value>me</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>init</servlet-name>
<url-pattern>/init.jsp</url-pattern>
</servlet-mapping>
then in my init.jsp I can get the init params using getServletContext.getInitParameter..
However, I am using WebSevlet 3.0 annotations and I can't see equivalent jsp-file annotation. I am looking for something along these lines
#WebServlet(initParams=#WebInitParam(name="hello", value = "hello"),description = "A Simple Servlet", urlPatterns = { "/init.jsp" })
where I can use jsp-file annotation. So I need to set the jsp init params without using web.xml.
I know you said without using a web.xml - but you could still use a web.xml file with annotations - and just use it for the parameters - define the init params as a context-param. That would set the parameter for the whole application...
<context-param>
<param-name>someParameter</param-name>
<param-value>someValue</param-value>
</context-param>
Then in your code - reference it in the same way as above :
getServletContext.getInitParameter("someParameter");
What is the difference between loading context in context param and loading it in init-param of Dispatcher Servlet.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
vs
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/mvc-config.xml </param-value>
</init-param>
What i understood is context-param is loaded by context listener and should only contain middle tier beans. Where as Dispatcher Servlet in its init method should load web tier beans. Is this understanding correct? Why do we load 2 things seperately?
<context-param>
Is written outside <Servlet> tag and is inside <webapp> tag.
The values decalred will be available to the whole application
Any servlet in the application (declared in the web.xml) can access the values
So we use this when we want to share the the same set of values across the servlet in the application such as Data base configuration details.
You can use public String getInitParameter(String name) method of ServletContext interface to get value.
getServletContext() method of ServletConfig interface returns the object of ServletContext.
getServletContext() method of GenericServlet class returns the object of ServletContext.
Example 1 : ServletContext application=getServletConfig().getServletContext();
Example 2 : ServletContext application=getServletContext();
<init-param> .
Is written inside <Servlet> tag.
The values declared will be available only to the servlet.
You can use public String getInitParameter(String name) method of ServletConfig interface to get value.
getServletConfig() method of Servlet interface returns the object of ServletConfig.
Example : ServletConfig config=getServletConfig();
In the context-param "contextConfigLocation" you should include your application contexts, as you have already said middle tier beans, like: services, datasource...
The Spring DispatcherServlet will look for config files in WEB-INF/servletName-servlet.xml. Using the init-param you can change this default behaviour. The servlets contexts (web contexts) are isolated but might hold the application contexts as parent. You can use both or one of them independently.
Thanks to everyone in advance,
I am trying to access any context parameters in the web.xml from within a servlet filter. Below is a portion from my web.xml file. I have verified that the context-param node is accessible via a jsp page using out.print(getServletContext().getInitParameter("error"));.
<filter>
<filter-name>prePost</filter-name>
<filter-class>MyFilter</filter-class>
<init_param>
<param_name>error</param_name>
<param_value>/test.jsp</param_value>
</init_param>
<filter-mapping>
<filter-name>prePost</filter-name>
<url-pattern>*.jsp</url-pattern>
<context-param>
<description>Error Handler</description>
<param-name>error</param-name>
<param-value>/test.jsp</param-value>
In my filters doFilter when I output this.filterConfig.getInitParameter("error"), I always get null. In my filters init() I am setting this.filterConfig with the passed in FilterConfig.
Thanks,
Sam
You're using underscores rather than hyphens for "param-name" and "param-value". Your config should look like this:
<init-param>
<param-name>error</param-name>
<param-value>/test.jsp</param-value>
</init-param>